WHY CANNOT WE PASS DATATABLE TO A WEBSERVICE?

Posted by ANU_DGR8 under Regular Expressions on 2/24/2011 | Points: 10 | Views : 7603 | Status : [Member] | Replies : 11
Can anybody tell me whether we can PASS DATATABLE TO A WEBSERVICE? if yes,how?if not,why not?

Thanks and Regards,
Hoosha Boosha



Responses

Posted by: Vuyiswamb on: 2/24/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
i dont understand your question

Thank you for posting at Dotnetfunda
[Administrator]

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krkc on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
Webservices can return any object that can be serialized.

And, YES we can return DataTable from any WebMethod because DataTable itself can be serialized.

The wsdl contains a reference to the entire XSD and the fact that you passed a dataTable is only mentioned in additional attributes, which means that only .NET clients will be able to identify the returned value and act accordingly.

Thanks,
Ravi

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ANU_DGR8 on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
@Vuyiswamb: I mean can we pass Datatable to a Web Service method? if yes,how..if not why?

Thanks and Regards,
Hoosha Boosha

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ANU_DGR8 on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
@Ravi: I am not talking about which all objects can webmethod return..but which all objects can the web method receive?

Thanks and Regards,
Hoosha Boosha

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krkc on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
@ANU .. again the answer is simple. Any object that can be serialized can be sent to your web method.

Thanks,
Ravi

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/25/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
A web service is still a a normal function , the only difference is that i can be accessed via web. So what you could do via normal function created in a Dll, you could do it via a webservice , the article below i used a dataset or rather i returned a Dataset or Datatable from a web service,

e.g You can have a webservice that accept a datatable like this


   [WebMethod(EnableSession = true)]

public bool UpdateData(System.Data.DataTable dt)
{
bool UpdateStatus = false;

if (dt.HasErrors == false)
{
UpdateStatus = true;
//Update the Dataset
}
else
{
UpdateStatus = false;
}
return UpdateStatus;
}


This above web service accepts a Datatable and return a Boolean if the Update is done or not. So its simple like that.



Thank you for posting at Dotnetfunda
[Administrator]

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
thanks both of you...now a question for dataset...how do i read the column values of dataset which is passed in the same way to the webservice?

Thanks and Regards,
Hoosha Boosha

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/25/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down

ds.Tables[0].Rows[0][0].ToString();


here i am accessing the first table in the dataset on the first row on the first column

Thank you for posting at Dotnetfunda
[Administrator]

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/25/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You can also specify

ds.tables["tableName"].Rows[0]["ColumnName"].ToString();

Thanks and Regards
Madhu

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
using System;

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Entity_Card;
using BAL_Card;

namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public string Add(DataSet ds)
{
string result ="false";
Entity_Card_Input objEntity = new Entity_Card_Input();
BL_Card_Input objBL = new BL_Card_Input();

for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{



objEntity.PONo = Convert.ToInt64(ds.Tables[0].Rows[i]["EBELN"].ToString());
objEntity.PRNo = Convert.ToInt64(ds.Tables[0].Rows[i]["BANFN"].ToString());
objEntity.Amt_PO = Convert.ToInt64(ds.Tables[0].Rows[i]["NETPR"].ToString());
result = objBL.Insert_PO_WebService(objEntity).ToString();

}
return result;

}



}
}

Additional information: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToInt64(String value)
at WebService1.Service1.Add(DataSet ds) in asmx.cs file line 46
--- End of inner exception stack trace ---

i dont understand why i am getting this kind of error,when i run my windows application..Please Help!


Thanks and Regards,
Hoosha Boosha

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krkc on: 2/25/2011 [Member] Starter | Points: 25

Up
0
Down
Input string was not in correct format.. this error is because when you try to convert a blank string "" to int.. or when the expected param is INT but you send string..

try debugging more into this..

check if your dataset / datatable is holding correct values.

Thanks,
Ravi

ANU_DGR8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response