Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebService - returing object with a list?

Status
Not open for further replies.

mdmohideen

Programmer
Mar 5, 2002
5
US
Hi,
I am working on creating a webservice in C#. Here is a scenario(sample code) of what I am trying to accomplish.

public class Customer {
public int id;
public string name;
public ArrayList list = new ArrayList(); //list for orders of a customer


}

public class Orders{
public int id;
public string orderno;
}


public class Service1{

[WebMethod]
public Customer getCustomerAndOrders(int customerId){

return customer;

}
}

The getCustomerAndOrders(..) method should return customer object along with list of Orders(ArrayList). The problem lies in returning the list along with the customer object.

I would appreciate your help.

Thanks,
-M




 
You could wrap the Customer and Orders objects into a DataSet. It has an overhead, but you can return different types of objects in the same call:
Code:
DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("Customer", typeof(object)));

dt.Columns.Add(new DataColumn("Orders", typeof(object)));

DataRow dr = dt.NewRow();

dr[0] = customerObject;

dr[1] = ordersArrayList;

dt.Rows.Add(dt);

DataSet ds = new DataSet();

ds.Tables.Add(dt);

return ds;
You can then just cast the objects back when the DataSet is returned. The beauty of this is that the DataSet handles all the serialization for you.
 
It is highly resommended NOT to use arraylist but use arrays instead. This because other languages could and will have difficulty to understand them. And the whole point of webservices is that you can read them from any language you prefer. So do a ToArray and you should be fine.

But you program can still use the arraylist to work with.

Christiaan Baes
Belgium

"My new site" - Me
 
Sorry guys, ignore my previous post. It appears that the DataSet doesn't handle the serialization for you.

Could have sworn that I've done this before....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top