Return multiple rows from asmx service

I have a web service method that I would like to return multiple rows from datatable.

I am familiar with returning values ​​from web service methods, but not with multiple rows from datatable. What is the best way to do this? Do I need to return an array or list<>?

My code method is configured as follows.

[WebMethod]
public void UpdateBold(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);

// return code here
}
+5
source share
2 answers

You can create a new type for your data table and return an 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:

. http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ - JSON, http://msdn.microsoft.com/en-us/library/bb763183.aspx

+8

DTO.

Ex, DTO:

public class Example
{
    public string Name { get; set; }
    public int Value { get; set; }
}

-:

[WebMethod]
public Example[] GetExamples()
{
      return new Example[]{
          new Example { Name = "Test", Value = 100 },
          new Example { Name = "Test 2", Value = 500 }
      };      
}
+6

All Articles