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. namespace WebServicewithpassingliat
  8. {
  9. [WebService(Namespace = "http://tempuri.org/")]
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11. [System.ComponentModel.ToolboxItem(false)]
  12. // [System.Web.Script.Services.ScriptService]
  13. public class Service1 : System.Web.Services.WebService
  14. {
  15. [WebMethod]
  16. public List<Mobiledetails> Allmobilelist()
  17. {
  18. Mobiledetails md = new Mobiledetails();
  19. md.Phonename = "Nokia X";
  20. md.PhoneOs = "Nokia X platfrom";
  21. md.Phoneprice = 10000;
  22. Mobiledetails md1 = new Mobiledetails();
  23. md1.Phonename = "Nokia XL";
  24. md1.PhoneOs = "Nokia X platfrom";
  25. md1.Phoneprice = 10000;
  26. List<Mobiledetails> ob = new List<Mobiledetails>();
  27. ob.Add(md);
  28. ob.Add(md1);
  29. return ob;
  30. }
  31. }
  32. }

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. namespace WebApplication2
  11. {
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. getlist();
  17. }
  18. public void getlist()
  19. {
  20. try
  21. {
  22. localhost.Service1 mo = new WebApplication2.localhost.Service1();
  23. // Created object of Webservice
  24. List<localhost.Mobiledetails> list = new
  25. List<localhost.Mobiledetails>(mo.Allmobilelist());
  26. //Created List
  27. string Name, Os;
  28. decimal Price;
  29. StringBuilder builder = new StringBuilder();
  30. foreach (Mobiledetails li in list)
  31. {
  32. Name = li.Phonename;
  33. Os = li.PhoneOs;
  34. Price = li.Phoneprice;
  35. builder.Append("Name :- " + Name + "</br>"
  36. + "OperatingSystem :- " + Os + "</br>"
  37. + "Mobileprice :- " + Price + "</br>");
  38. }
  39. Literal1.Text = builder.ToString();
  40. }
  41. catch (Exception)
  42. {
  43. throw;
  44. }
  45. }
  46. }
  47. }

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.