Webservice to return a table or Dataset

Posted by Sksamantaray under ASP.NET on 1/8/2012 | Points: 10 | Views : 9517 | Status : [Member] | Replies : 4
Can someone give a sample of Webservice which returns a datatable or dataset ?

Thanks,
Sanjay



Responses

Posted by: Vuyiswamb on: 1/8/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Do you know how to write a normal function that returns a dataset ?

Then there is no difference , the only thing difference is that on top of the Function name declaration , there will be a tag

[webmethod]



Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Thiyagaa on: 1/10/2012 [Member] Starter | Points: 25

Up
0
Down
[WebMethod()]
public string[] Method_PlaitiffName(string prefixText, int count)
{
string SelectQry = "SELECT distinct cc.Name "+
"FROM tbl_AssignTask assign "+
"INNER JOIN tbl_Judgements judgements ON assign.Judgementid = judgements.Judgementid "+
"INNER JOIN tbl_ProcessStages PS ON PS.Stageid=assign.stageid "+
"inner join tbl_contactlink cl on assign.Documentid =cl.Documentid "+
"inner join tbl_Contacts cc on cl.contactid =cc.contactid "+
"WHERE assign.stageid=10 AND Completed=1 and cc.CType=2 and cc.Name like '" + prefixText + "%'" +
"ORDER BY cc.Name";

ArrayList Results = new ArrayList();
try
{
using (SqlCommand Command = new SqlCommand(SelectQry, Connection))
{
using (SqlDataReader Reader = Command.ExecuteReader())
{
int Counter = 0;

while (Reader.Read())
{
if ((Counter == count))
break;
Results.Add(Reader["Name"].ToString());
Counter += 1;
}


}
string[] ResultsArray = new string[Results.Count];


ResultsArray = (string[])Results.ToArray(typeof(string));
return ResultsArray;
}
}
catch (Exception ex)
{
throw ex;
}
}

Replace string[] with DataTable object and change the function to return datatable.


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

Posted by: Vuyiswamb on: 1/10/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
look at this

[WebMethod()] 

public DataTable Method_PlaitiffName(string prefixText, int count)
{
string SelectQry = "SELECT distinct cc.Name "+
"FROM tbl_AssignTask assign "+
"INNER JOIN tbl_Judgements judgements ON assign.Judgementid = judgements.Judgementid "+
"INNER JOIN tbl_ProcessStages PS ON PS.Stageid=assign.stageid "+
"inner join tbl_contactlink cl on assign.Documentid =cl.Documentid "+
"inner join tbl_Contacts cc on cl.contactid =cc.contactid "+
"WHERE assign.stageid=10 AND Completed=1 and cc.CType=2 and cc.Name like '" + prefixText + "%'" +
"ORDER BY cc.Name";
Sqldataadapter da = new Sqldataadapter ();
SqlCommand Command = new SqlCommand(SelectQry, Connection)
da.selectedCommand = Command;
DataTable dtresults= newDataTable ();
try
{
Connection.Open();
da.Fill(dtresults);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Connection.Close();
}
return dtresults;
}


Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Thiyagaa on: 1/11/2012 [Member] Starter | Points: 25

Up
0
Down
ya its correct. you can use this web method in your web page.

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

Login to post response