Want to start implementing a couple of impressive Web services to gain
Exposing Database Information, the Quick and Easy Way
Five Things You Need to Do Before Publicizing Your Web Service
Improving Performance with Easy Caching
Online Translations: Get Your Programs Speaking Spanish in Seconds!
Adding Google Search to Your Programs
Querying the Amazon.com Web Service
View the Real World in Your Application, with TerraServer
Asynchronous Access: Secrets of Calling a Web Service in the Background
Web Services, Proxies, Connection Closed ”Oh My!
Change a Web Service URL, Without Recompiling
Where to Find the Best Web Services Around
Let s get busy!
One of the problems that most Web service developers soon run into is... well, they can be a little limiting. A function may return a True or False , a number or a string. But that s not very much, really, is it?
The clever developers pass about
smart data
. By that, I mean, instead of a simple data type, they pass back something containing more than just one piece of information. A structure (
However, one of the most useful types of smart data is the DataSet ”and here we ll be looking at how to expose the information in a database table through a Web service, allowing your users to view and update, in just a few lines of code. Here s how .
Create your Web service, remembering to note the location. Using the Server Explorer (View
Server Explorer), connect to a database and drag and drop the table you d like to expose through this Web service onto your Service1.asmx page. (You may rename your page, but, for simplicity in this example, we re
You should see two objects ”a Connection and DataAdapter ”created for you. The Connection object connects into the database, whereas the DataAdapter talks to the database through the connection, sending and retrieving information. You can learn more about these objects in Chapter 4.
Right-click on your
DataAdapter
and select Generate Dataset. Here, you ll be creating a template
DataSet
class, which you can create instances from. Edit the new
DataSet
name if required, and make a note for future reference. (For this sample, I m going to call mine MyDataSetTemplate.)
Figure 5-1:
Setting up our initial data objects
You may not realize it, but two things have
Next, we need to add code to perform two simple actions: firstly, code to allow our client to retrieve data from the table; secondly, code to allow our client to pass back an edited DataSet for updating of the database. Press F7 to view the code window and add code similar to the following to your service:
<WebMethod()> Public Function GetData() As MyDataSetTemplate
' Passes populated typed DataSet back to client
MyDataAdapter.Fill(MyDataSet)
Return MyDataSet
End Function
<WebMethod()> Public Sub UpdateData(ByVal Data As MyDataSetTemplate)
' Updates the backend database where needed
MyDataSet.Update(Data)
End Sub
When you re ready, click on Build
Build Solution to compile your Web service. You can also press F5 (or click on Debug
Start) to view the Web interface to your service, should you wish to do so. And that s it! You ve built your server, and ”with just a few lines of code ”created a retrieval and update mechanism for your table data.
So, the server is complete. Now we need to work on our client, and this bit is even easier.
Create a new application of any type; a Windows application is
Add Web Reference and type in the location of the Web service ASMX file you just edited, such as http://localhost/mywebservicelocation/
Service1.asmx
, and then press Return. When details of your service have finished loading, click on Add Reference. This will create a wrapper class for the Web service, allowing you to access its
Next, I m going to add a little code to my application to access the data through my Web service. Yours may be similar, or completely different. The following snippet, however, should give you a decent idea of how the
' Declare Service and DataSet class Dim MyWebService As New localhost.Service1() Dim MyDataSet As localhost.MyDataSetTemplate ' Grab data MyDataSet = MyWebService.GetData ' Retrieve sample data note the