WebService returning a DataTable problem

Source: Internet
Author: User

Today, when doing the project, want to return to the DataTable in the WebService, in the unit did not succeed, see online Some people say datable in. net1.1 is not serialized, and cannot be returned directly in WebService, You can return a dataset. At night, I went to the internet to find a bit, see the Basic agreement, there is a lot of articles quoted in the asp.net2.0 that the DataTable has been serialized, But you also need to manually write some code to return from the webservice. I just want to write another code? Continue to search, found that some people say must give datable name can return, but also can be manually converted to the DataTable type on the caller. Did it again, completely, Without forcing the type to convert. The code is as follows:

In the WebService file

[WebMethod]
Public DataTable DT ()
{
DataTable dt = new DataTable ("Default");
Dt. Columns.Add ("id");
Dt. Columns.Add ("name");
for (int i = 0; i <; i++)
{
DataRow dr = dt. NewRow ();
dr["id"] = i.tostring ();
dr["name"] = "name" + i.tostring ();
Dt. Rows.Add (DR);
}
return DT;
}

In the call page

Webservice1.webservice1 service = new Webservice1.webservice1 ();
DataTable dt = Service.dt ();
Gvuser.datasource = DT;
Gvuser.databind ();

       The key is to name the DataTable in the WebService method, otherwise it will be an error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.