I have a web service method which i'd like to return multiple rows from a datatable.
I am familiar with returning values from web service methods but not multiple rows from a datatable. What is the best way to go about doing this? Do I need to return an array or list<>
?
My code method is setup like this.
[WebMethod]
public void UpdateBold(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
// return code here
}
You can create new type for your data table item and return array of this data
public class sample
{
public string val1;
public string val2;
}
[WebMethod]
public sample[] UpdateBold(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
var samples = new List<sample>();
foreach(DataRow item in dt.Rows)
{
var s = new sample();
s.val1 = item[0].ToString();
s.val2 = item[1].ToString();
samples.Add(s);
}
return samples.ToArray();
}
for Ajax:
for consuming see http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ but you should make your webservice to serialize JSON, for this see http://msdn.microsoft.com/en-us/library/bb763183.aspx
I would return an Array of light-weight DTOs.
Ex, DTO:
public class Example
{
public string Name { get; set; }
public int Value { get; set; }
}
And in your web serivce:
[WebMethod]
public Example[] GetExamples()
{
return new Example[]{
new Example { Name = "Test", Value = 100 },
new Example { Name = "Test 2", Value = 500 }
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With