What to do if I want to return rows from different tables in ASP.NET WebService
-
I am trying to make and Android app with ASP.NET WebService. In WebService, if I want to return values from a single table I know what to do :
public DataTable GetUserDetails(string userName)
{
....
return userDetailsTable;
}But I don't know what to do if I want to return values from different tables (a more complex data). Should I use
List
or something else? If I use
List
, how should I handle it in Android side? Thanks.
-
I am trying to make and Android app with ASP.NET WebService. In WebService, if I want to return values from a single table I know what to do :
public DataTable GetUserDetails(string userName)
{
....
return userDetailsTable;
}But I don't know what to do if I want to return values from different tables (a more complex data). Should I use
List
or something else? If I use
List
, how should I handle it in Android side? Thanks.
Returning things like DataTable is generally a bad idea, especially if your client needs to be lean and use minimal data resources. When you return a DataTable you get a *lot* of stuff returned, when you are normally just interested in the data. What people normally do is define a class object that represents the bits of data you want to return. So if you want to return a Customer table you'll create a Customer class with properties like ID, FirstName, Surname etc, then in your web method you create a List and for each row of your datatable you'll create a Custom object, populate the properties from the fields, add it to the List and then return the List. This will greatly reduce the amount of data sent to your app, and your app now just sees a simply collection with just the data it needs.
-
Returning things like DataTable is generally a bad idea, especially if your client needs to be lean and use minimal data resources. When you return a DataTable you get a *lot* of stuff returned, when you are normally just interested in the data. What people normally do is define a class object that represents the bits of data you want to return. So if you want to return a Customer table you'll create a Customer class with properties like ID, FirstName, Surname etc, then in your web method you create a List and for each row of your datatable you'll create a Custom object, populate the properties from the fields, add it to the List and then return the List. This will greatly reduce the amount of data sent to your app, and your app now just sees a simply collection with just the data it needs.
Thank you very much for your reply. Can you tell me how I should make Android could handle List< Customer >? A sample code would be great. Moreover is it a good idea to send List< Customer > and List< Order > at the same time? Thanks a lot.
-
Thank you very much for your reply. Can you tell me how I should make Android could handle List< Customer >? A sample code would be great. Moreover is it a good idea to send List< Customer > and List< Order > at the same time? Thanks a lot.
No idea TBH, this is a c# forum, I don't know anything about Android apps. Although you are return a List, .net will convert that to json so your Android app should see something like [{ID:1, FirstName: "John", LastName: "Doe"}, {ID=2, FirstName: "Dave", LastName: "Smith"}] whatever framework Android has for calling web services will know how to parse that.