Article source code: websvc_dataset.zip
This article will demonstrate how to pass an ADO.NET DataSet from a server to a client. The server will pass the DataSet through a Web Service (therefore IIS will be required on the server). You will be able to view the DataSet through any standard web browser such as Internet Explorer, Opera, or Netscape Navigator. A simple Windows SmartClient will also be created in order to show how to take the DataSet and use it directly in standard Windows components such as data grids.
What are Web Services?
Web Services allow programmers to easily solve one of the fundamental problems facing many programmers today: how to get information from "here" to "there". Over the years there have been many different ways to move data between systems (including FTP, ODBC, Mapped Drives, DCOM, etc…) but no previous method has successfully bridged the "ease of use", "open standards", and "scalability" issues as successfully as that of Web Services. Web Services are easy to code, based on open XML standards, and can be scaled both up and out in the same manner that IIS web servers are (by adding more processing power to existing hardware or adding additional servers in a web farm layout). Web Services, like COM objects, give you the ability to create blocks of logic and make them available to both yourself and other programmers.
Although championed by Microsoft, Web Services are by no means a Microsoft-only technology. One prime benefit of building a Web Service is that it may be consumed by other programmers regardless of their platform (Mac, Unix, Linux, PocketPC, etc). All data is sent through XML to the calling system. In this way you can continue to use VB.NET, C#, VB6, etc as your programming language of choice even though your business partners may be using PERL, PHP, or Java to connect to your systems. However, this article will focus exclusively on VB.NET as the programming language of choice.
Identifying a Web Service in Code
Marking a class as a Web Service in VB.NET is as simple as adding the identifier
<WebMethod()> immediately before your class definition. Since no changes are required in your main code the migration path to Web Service development is relatively straightforward.
Sending a DataSet through a Web Service
Using a Web Service to send ADO.NET data is just as easy as sending traditional simple data types (such as strings or integers). Your data may be pulled in from a database, an xml file, or built in memory using ADO.NET's advanced data building commands. Samples of each method are included with this article. You will note that each sample follows the same basic template:
<WebMethod()> Public Function theFunctionName() as DataSet
dim ds as DataSet
MS Access 2002 DataSet Example
<WebMethod()> Public Function ListPhoneNumbers_Access() As DataSet
Dim myConnection As OleDbConnection
Dim myDataAdapter As OleDbDataAdapter
Dim myDataSet As DataSet
myConnection = New OleDbConnection _
("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + _
Server.MapPath("/") + "\PhoneNumbers\PhoneNumbers.mdb")
myDataAdapter = New _
OleDbDataAdapter("SELECT * FROM PhoneNumbers", myConnection)
myDataSet = New DataSet()
XML File DataSet Example
<WebMethod()> Public Function ListPhoneNumbers_XML() As DataSet
Dim ds As New DataSet()
ds.ReadXml((Server.MapPath("/") + _
<WebMethod()> Public Function ListPhoneNumbers_Default() As DataSet
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = ds.Tables.Add("PhoneNumbers")
Dim newRow As DataRow
newRow = dt.NewRow
newRow("name") = "Jeff Laughlin"
newRow("home") = "555-111-2222"
newRow("mobile") = "555-111-3333"
newRow("netWorth") = "Negligible"
newRow = dt.NewRow
newRow("name") = "Bill Gates"
newRow("home") = "555-222-2222"
newRow("mobile") = "555-222-3333"
newRow("netWorth") = "33.6 Billion"
newRow = dt.NewRow
newRow("name") = "Steve Balmer"
newRow("home") = "555-333-2222"
newRow("mobile") = "555-333-3333"
newRow("netWorth") = "15.1 Billion"
Deploying a Web Service
Much like a traditional website deploying a Web Service is not a difficult process though it is not without its potential hang-ups.
If you have built your Web Service through the Visual Studio.NET IDE then all local deployment issues have been taken care of automatically. If you wish to move your solution to another computer you can choose to deploy your service using Visual Studio.NET's build-in wizard or you can choose to copy the site to a folder on the other computer. If you choose to copy the site then you will need to perform one additional step using the IIS MMC interface. You will need to identify the folder as a Virtual Directory Application in order for the proper configuration settings to be handled by the ASP.NET processor. If you forget to designate the folder as an application you are likely to receive the following error message when attempting to load the URL into your web browser.
It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS
Testing the Web Service
Visual Studio.NET provides a very simple way to test Web Services using nothing more than a web browser. Specify the ASMX file in your web browser's address bar to connect to your Web Service and test your code.
Calling a Web Service DataSet in your own Windows Form application is just as easy as using a standard COM component was back in Visual Basic 6. You set a reference to the Web Service and program against it just as normal.
The SmartClient provided with this article demonstrates the ease of programming against a Web Service. Three buttons are included which retrieve data from the Web Service; one from an Access file, one from an XML file, and one from a DataSet created on-the-fly using ADO.NET code. One extra button is also included which sends a DataSet back to the Web Service and saves the data in an XML file. This update will fail unless the Web Service has permission to write to files in the appropriate folder.
Related devCity.NET articles: