Send DataSet and DataTable From Webservice and Consume in Application

I am writing simple instructions with snapshots for a good understanding and for your use in your projects.

Let's create our web service first for sending a DataSet and DataTable.

Step 1 : Open Visual Studio then select  "File" -> "New" -> "Project..." then provide a project name.



I have provided the project name WebServicewithpassingliat.

After adding the project name we will just create a new web service.

Step 2 : Let us add a web service for sending a DataSet and DataTable.

For adding the web service right-click on the solution then select Add > Add New Item then select Web Service then click the Add button.



Step 3 : After adding the web service you will see a WebMethod with the name Helloworld; just remove it.

And create a new WebMethod of type DataSet and name it MobileDataset() and add another Web Method of type DataTable and name it MobileDatatable().

For reference see the following image:


  1. DataSet Code Snippet.

    And this Webmethod will return a DataSet.

  2. DataTable code snippet.


And this Webmethod will return a DataTable.

Just run your application now.

You will see the same screen that is given below.



Now just copy this URL.

http://localhost:2157/Service1.asmx

Step 4 :
Let's create another project then it will consume this list.

Open Visual Studio then select "File" -> "New" -> "Project..." then provide a project name.

Here I am using the name WebApplication2.



After creating it you will see your solution like the following:



Step 5 : Then just right-click on the solution and select Add Web Reference.



After selecting Add Web Reference a new dialog will pop up asking for an address.



Step 6 : Just paste the preceding address that you copied.

http://localhost:2157/Service1.asmx



After adding the URL just click the Add Reference button.

Now your solution will look like this.



Now we have completed the adding of the service.

Step 7: Now we just want to use this service.

We have a default page that is created by default.

On the page load of that page I have created a couple of methods with the names:

  1. GET_DATASET(); // For Getting the Dataset
  2. GET_DATATABLE(); // for Getting the DataTable

Code of the default page:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using WebApplication2.localhost;  
  9. using System.Text;  
  10.   
  11. namespace WebApplication2  
  12. {  
  13.     public partial class _Default : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.             GET_DATASET();  
  18.             GET_DATATABLE();  
  19.               
  20.         }  
  21.   
  22.         public void GET_DATATABLE()  
  23.         {  
  24.             localhost.Service1 mo = new WebApplication2.localhost.Service1();  
  25.             DataTable dt = mo.MobileDatatable();  
  26.             GridView1.DataSource = dt;  
  27.             GridView1.DataBind();  
  28.         }  
  29.   
  30.         public void GET_DATASET()  
  31.         {  
  32.             localhost.Service1 mo = new WebApplication2.localhost.Service1();  
  33.             DataSet ds = mo.MobileDataset();  
  34.             GridView2.DataSource = ds;  
  35.             GridView2.DataBind();  
  36.         }  
  37.     }  

Namespace

  1. using WebApplication2.localhost; // is the namespace for accessing service objects 

In this code I have created an object of web service that we have added.

  1. localhost.Service1 mo = new WebApplication2.localhost.Service1();   

After creating an object I have passed an object to the DataSet.

  1. localhost.Service1 mo = new WebApplication2.localhost.Service1();  
  2. DataSet ds = mo.MobileDataset(); 

For the DataTable I have created an object. I have passed an object to the DataTable.

  1. localhost.Service1 mo = new WebApplication2.localhost.Service1();  
  2. DataTable dt = mo.MobileDatatable(); 

Design of Default page

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.     <div>  
  8.         <asp:GridView ID="GridView1" runat="server">  
  9.         </asp:GridView>  
  10.         <br />  
  11.         <asp:GridView ID="GridView2" runat="server">  
  12.         </asp:GridView>  
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </html> 

I have only added 2 GridViews for displaying data of both the DataTable and DataSet.

After doing that, just run the application and check it.

Here is the output:



Now we have successfully transferred a DataTable and DataSet.

Up Next
    Ebook Download
    View all
    Learn
    View all