Sending List From WebService and Consume it in Application

In this article we will see how to send a List<> from an ASP.NET Webserivce and consume a List<> in our application.

I am providing a simple procedure with snapshots for a good understanding and for you to use in your projects.

Let's create our webservice first for sending a List.

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

project Name

Step 2: Let us add a class with the name Mobiledetails.cs.

Inside this class we will do Get { } and Set { } Parameters.

Step 3: Also create another class inside Mobiledetails.cs with the name Mobiledetailslist.

Which contains List<> of Mobiledetails ( List<Mobiledetails> ).

You can see the image below for reference.

reference

Step 4: Let us add a Webservice for sending the list.

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

Web service

Step 5: After adding Webservice you will see WebMethod with the name Helloworld; just remove it.
 
And a new WebMethod of generic type List<Mobiledetails> and name it as Allmobilelist().

This WebMethod will return a List of Mobiledetails.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.Data;  
  7.   
  8. namespace WebServicewithpassingliat  
  9. {  
  10.      
  11.     [WebService(Namespace = "http://tempuri.org/")]  
  12.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  13.     [System.ComponentModel.ToolboxItem(false)]  
  14.    // [System.Web.Script.Services.ScriptService]  
  15.     public class Service1 : System.Web.Services.WebService  
  16.     {  
  17.         [WebMethod]  
  18.         public List<Mobiledetails> Allmobilelist()  
  19.         {  
  20.             Mobiledetails md = new Mobiledetails();  
  21.             md.Phonename = "Nokia X";  
  22.             md.PhoneOs = "Nokia X platfrom";  
  23.             md.Phoneprice = 10000;  
  24.   
  25.             Mobiledetails md1 = new Mobiledetails();  
  26.             md1.Phonename = "Nokia XL";  
  27.             md1.PhoneOs = "Nokia X platfrom";  
  28.             md1.Phoneprice = 10000;  
  29.   
  30.             List<Mobiledetails> ob = new List<Mobiledetails>();  
  31.             ob.Add(md);  
  32.             ob.Add(md1);  
  33.             return ob;  
  34.         }  
  35.     }  

In this webmethod I have created an object of class Mobiledetails and accessed its properties for assigning a value.

After assiging a value to the properties I created a List <Mobiledetails> object and added properties to the List. And finally returing a list.

Now we have completed the part of sending a List.

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

URL

Step 6: Let's create another project to consume this List.

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

Here I am giving the name WebApplication2.
 
Name

After creating it your solution will look as in the following.
 
solution

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

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

Just put the preceding address that you have copied.

http://localhost:2157/Service1.asmx
 
address 
 
After adding the URL just click the button Add Reference.

Now your solution will look like this.

your solution

Now we have completed adding the service.

We just want to use this service.

We have a Default Page that is created by default.

On page load of that page I have created a Method named Getlist().
 
Method Name

Code of 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.             getlist();  
  18.         }   
  19.         public void getlist()  
  20.         {  
  21.             try  
  22.             {  
  23.                 localhost.Service1 mo = new WebApplication2.localhost.Service1();    
  24.                 // Created object of Webservice    
  25.                List<localhost.Mobiledetails> list = new   
  26.                  List<localhost.Mobiledetails>(mo.Allmobilelist());                            
  27.                //Created List    
  28.                 string Name, Os;   
  29.                 decimal Price;  
  30.                 StringBuilder builder = new StringBuilder();  
  31.   
  32.                 foreach (Mobiledetails li in list)  
  33.                 {  
  34.                     Name = li.Phonename;   
  35.                     Os = li.PhoneOs;   
  36.                     Price = li.Phoneprice;  
  37.   
  38.                     builder.Append("Name :- " + Name + "</br>"   
  39.                     + "OperatingSystem :- " + Os + "</br>"  
  40.                     + "Mobileprice :- " + Price + "</br>");  
  41.                 }  
  42.                 Literal1.Text = builder.ToString();  
  43.             }  
  44.             catch (Exception)  
  45.             {  
  46.                 throw;  
  47.             }  
  48.         }  
  49.     }  

Namespace

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

In this code I created an object of the webservice that we added.

  1. localhost.Service1 mo = new WebApplication2.localhost.Service1();  
  2. After creating object I have created a list of Mobilesdetails ( List<localhost.Mobiledetails> )  
  3. and add object of mo.Allmobilelist();  
  4. List<localhost.Mobiledetails> list = new List<localhost.Mobiledetails>(mo.Allmobilelist()); //Created List  

I used a loop for getting the data from the List and a StringBuilder for displaying the formatted data.

The last thing is to add a Literal for displaying this list.

After completing, just run the application and check it.

Here is the output:

output

Now we have successfully transferred this list.