Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return multiple rows from an asmx service

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
}
like image 830
tmutton Avatar asked Dec 05 '10 16:12

tmutton


2 Answers

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

like image 92
Saeed Amiri Avatar answered Sep 25 '22 05:09

Saeed Amiri


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 }
      };      
}
like image 21
Robin Orheden Avatar answered Sep 25 '22 05:09

Robin Orheden