Webservice ASP.NET with GridView and DataGridView
Section: XML WebservicesRating: Not rated yet!

Add to YiGGAdd to google-bookmarksAdd to linkarenaAdd to redditAdd to del.icio.usAdd to misterwongAdd to digg


zip-attachment Attachment




Introduction

This article describes how to build a WebService with .NET and use Generics in this WebService to transfer a list of complex datatypes. A WebService can be used in a WinForms-client and in a Web-client as well. In our example a list of Person-objects is bound to a GridView in an ASP.NET-project and to a DataGridView in a WinForms-project. The goal of this article is to demonstrate, how you can use a WebService with two different type of views: WinForm and WebForm.


Figure 1

First the WebService


Creating a WebService in .NET is very simple: Choose File->New->Web Site in VS 2005 and then ASP.NET Web Service. In our example we call the project Persons. Now a class named Service is created for us, rename it to PersonService. Cause we want to transfer data of persons, we create a Person-class in the App_Code-Folder:

namespace Persons { public class Person { public Person() { } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public string FirstName; public string LastName; } }

Now our project has the following structure:


Figure 2

Our WebService has just one method to transfer a list of Person-objects. It is defined like this in the file PersonService.cs:

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class PersonService : System.Web.Services.WebService { public PersonService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public List<Person> GetPersons() { List<Person> persons = new List<Person>(); persons.Add(new Person("Mark", "Twain")); persons.Add(new Person("Tom", "Taylor")); persons.Add(new Person("Richard", "Bachmann")); return persons; } }

As you an see we only transfer some sample data. In a real-world application the data would come from a database.
The result of the method is a Generic-List with Person-objects.

Note: The Person-type needs a parameterless constructor to be serializable.

To test the WebService just choose View in browser on the PersonService.asmx-file:


Figure 3

The ASP.NET-project (WebSite)


To bind the data to a GridView in an ASP.NET-project we create a ASP.NET-WebSite and drop a GridView on the WebForm of the designer-generated page Default.aspx.


Figure 4

In the Page_Load-eventhandler of the Default.aspx-page we get the data from our PersonService and bind it to the GridView. The result of the method is an array of Person-objects. This means our Generic-List with Person-instances has been "translated" to an array to get serialized through the WebService.

protected void Page_Load(object sender, EventArgs e) { PersonService p = new PersonService(); Person[] persons = p.GetPersons(); gridViewPersons.DataSource = persons; gridViewPersons.DataBind(); }

But how is the PersonsService introduced? Easy. Just add a Web reference to the PersonService in the solution:


Figure 5


Figure 6

The columns in the GridView are generated automatically if you set the property AutoGenerateColumns to true.

The WinForms-client


To display the data of our WebService in a WinForms-Client, we need to add a Windows-Application to our solution (File->Add->New Project and then Windows-Application).

To the Form the designer generated for us, drop a DataGridView from the toolbox-Tab All Windows Forms and a button with a Click-eventhandler in which we call the method GetPersons of the PersonsService and bind the result to the DataGridView:

private void btnFill_Click(object sender, EventArgs e) { PersonService p = new PersonService(); Person[] persons = p.GetPersons(); dataGridView1.DataSource = persons; }

Then choose Add Reference with the name Persons. That?s all, here is how the result looks like:


Figure 7

So what is the benefit of the approach?


As I explained you can use one interface - the PersonService - in two different kind of projects, a client-application and a Web-application. If your are developing a framework or other kind of API for customers, it makes no difference for them in which environment they use your component. If they are planning to design their application with WPF - no probem, they can use the same code. The logic resides on the server and every client is able to use it, cause it has a standardized protocol.

The project is attached, feel free to extend it. Have fun!




 Reader-Comments:

CodeGod
7/21/2007 12:24:51 PM

2 Bugs in code

Hi, I found two bugs in the code tha will be corrected soon.

1. Person has to use properties
2. Result of the call GetPersons should be a List