Article

.NET Web Services Made Easy

Page: 1 2 3 4 5 6 Next

Build an Interactive Web Service

So, here we go then. This time, we're going to create a fully functional Web Service. But what will it do? Well, we're going to create a Web Service that exposes two WebMethods, randomQuote() and addQuote(). Our Web Service will be a simple quote system, offering users the ability to have a section on their site that will display a random quote and provide users with the ability to add new quotes.

Before we get started, create a new folder in your Web root (usually c:\intetpub\wwwroot) called quoteWS, and turn it into a new virtual directory.

The Database

We're going to create a simple database to hold the quotes, and design it in a way that will allow you to easily change it to an Access database. Create a new database and call it quoteDatabase. Here's what the schema looks like:

993_ws4

Once you've made the table, save it as tblQuotes.

Now we've made the table, let's look at our first WebMethod, randomQuote()

randomQuote() Web Method

Before we start our start our WebMethods we have to set up the class we want to expose:

<%@ WebService Language="VB" Class="Quote" %>  
 
Imports System.Web.Services  
Imports System  
Imports System.Data  
Imports System.Data.SqlClient  
 
 
<WebService(Namespace:="http://www.sitepoint.com/Quote.")> _  
Public Class Quote  
   
 ' Private string to hold the connection details  
 Private strConnection As String    
 = "server=localhost;database=quoteDatabase;uid=sa;pwd=;"  
 
End Class

Save the file as quote.asmx. Everything you see should be familiar; first we have the @WebService directive, and then we import the Namespaces for the classes and methods we're going to use. Next we declare our XML namespace, and the class we're going to expose. The only line we have inside the class is a private string that holds our database details.

With the basic bones our service laid down, let's look at our first WebMethod.

<WebMethod(Description:="Returns a random quote    
on each request in the form of a DataSet")> _  
 Public Function randomQuote() As DataSet  
   
   Dim objDataAdapter As New SQLDataAdapter '    
   DataAdapter that will get the data from the  
   Dim objConnection As New SQLConnection(strConnection) '    
   Connection object that will allow the DataAdpater to    
   connect to the    
   Dim DS As New DataSet() ' Will store the data  
   Dim returnDS As New DataSet() ' Will be the DataSet    
   that is returned  
     
   Dim quoteID As Integer ' Will hold the ID number    
   of the random quote  
     
   objDataAdapter = New SQLDataAdapter("SELECT * FROM    
   tblQuotes", objConnection)  
     
   objDataAdapter.Fill(DS, "quoteTable")  
     
   quoteID = DS.Tables("quoteTable").Rows.Count * Rnd()  
     
   Dim tmpDataTable As New DataTable("returnRow")  
   tmpDataTable = DS.Tables("quoteTable").Clone  
   tmpDataTable.ImportRow(DS.Tables("quoteTable").Rows(quoteID))  
     
   returnDS.Tables.Add(tmpDataTable)  
     
   Return returnDS  
 End Function

Let's quickly review the code. When we declare the WebMethod, we supply a description, the function name, and what we're going to return.

<WebMethod(Description:="Returns a random quote    
on each request in the form of a DataSet")> _  
 Public Function randomQuote() As DataSet

The next few lines declare the objects and variables we're going to use:

Dim objDataAdapter As New SQLDataAdapter '    
DataAdapter that will get the data from the  
Dim objConnection As New SQLConnection(strConnection) '    
Connection object that will allow the DataAdpater to    
connect to the database  
Dim DS As New DataSet() ' Will store the data  
Dim returnDS As New DataSet() ' Will be the DataSet    
that is returned  
     
Dim quoteID As Integer ' Will hold the ID number    
of the random quote

The next lines return the data from the database:

objDataAdapter = New SQLDataAdapter("SELECT * FROM    
tblQuotes", objConnection)  
objDataAdapter.Fill(DS, "quoteTable")

Now that we have a DataSet containing our quotes, we have to select one of them randomly. For this we use the row numbers from the DataSet and the rnd() function. There are better, more reliable ways to do this, but for speed and ease, we're going with this option.

quoteID = DS.Tables("quoteTable").Rows.Count * Rnd()

We then create a DataTable to hold the row we want to return. We clone the table that's holding the current quotes, and then copy the row over.

Dim tmpDataTable As New DataTable("returnRow")  
tmpDataTable = DS.Tables("quoteTable").Clone  
tmpDataTable.ImportRow(DS.Tables("quoteTable").Rows(quoteID))

Because we can't return the DataTable through the Web Service, we have to insert it into a DataSet.

returnDS.Tables.Add(tmpDataTable)

Then finally we return the DataSet.

Return returnDS

With that, we've finished our WebMethod that returns our random quote! Why not surf along and test it out?

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links