Article
.NET Web Services Made Easy
In the first few lines we have our page directive and the Namespaces we want to import. As you can see, we are importing our Web Service proxy. This allows use easy access to the methods.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Quote" %>
Next we have our Page_Load function, which contains some code to kick up the generateQuote() function, but only if the page has not been posted back.
Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
If ( NOT isPostBack ) Then
generateQuote(obj, e)
End If
End Sub
We have our generateQuote() function now. This creates an instance of the Web Service, and creates a new DataSet using the returned data. Then we bind it to a repeater class.
Sub generateQuote(ByVal obj As Object, ByVal e As EventArgs)
Dim randomQuote As New quote.quote()
Dim getQuote As DataSet = randomQuote.randomQuote()
returnQuote.DataSource = getQuote
returnQuote.DataBind()
End Sub
Let's jump to the Repeater Web Control. We didn't have to use this Web Control, but for simplicity, we'll stick with it.
Inside the Control, we have a table that holds out returned quote -- quite simple really. You might notice, though, that one of the lines is slightly different.
<ASP:Repeater ID="returnQuote" runat="server">
<itemTemplate>
<table width="500" style="border: 1 solid #8080C0" class="body">
<tr>
<td>Quote Number: <%# DataBinder.Eval
(Container.DataItem, "quoteID") %></td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem,
"displayContent") %>
</td>
</tr>
<tr>
<td>
<%# checkWebSite(DataBinder.Eval
(Container.DataItem, "submittedBy"), DataBinder.Eval
(Container.DataItem, "website")) %>
</td>
</tr>
</table>
</itemTemplate>
</ASP:Repeater>
The line in bold is a little different to the rest. Instead of sending the data straight to the browser, it sends it to a function called checkWebSite(). This function checks to see if the website is a link. If it is, the function returns a link using the website and submittedBy value, and if it's not, it simply returns the submittedBy value.
Function checkWebSite(byVal submittedBy
As String, byVal website As String) As String
Dim returnString As String
If ( Regex.isMatch(website, "\b(www|http)\S+\b") ) Then
returnString = Regex.Replace(website,
"\b(www|http)\S+\b", "<a href=""$0"" target=""_blank"">"
& submittedBy & "</a>")
Else
returnString = submittedBy
End If
Return returnString
End Function
And that's how you consume the randomQuote() WebMethod! Nice and easy! Now, let's move onto the addQuote() WebMethod.
Consuming The addQuote() WebMethod
The addQuote() WebMethod allows the user to add quotes to the database. If you remember, the WebMethod didn't validate the data, so we'll include some simple validation.
Open a new document in your text editor and add the following:
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="Quote" %>
<script runat="server">
Sub addQuote(byVal obj As Object, byVal e As EventArgs)
If ( page.isValid) Then
Dim randomQuote As New quote.quote()
Dim addOK As Boolean = randomQuote.AddQuote
(quoteContentTxt.Text, submittedByTxt.Text, websiteTxt.Text)
If ( addOK ) Then
result.Text = "Quote was successfully added!"
quoteContentTxt.Text = ""
submittedByTxt.Text = ""
websiteTxt.Text = ""
Else
result.Text = "Quote was not added!"
End If
End If
End Sub
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<style>
.body {
FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, Helvetica
}
</style>
</head>
<body>
<form name="addQuoteForm" runat="server">
<ASP:Label id="result" CssClass="body" Font-Bold="true"
runat="server"></ASP:Label>
<table style="border: 1 solid #8080C0" class="body">
<tr>
<td valign="top">Quote Content:</td>
<td width="10"> </td>
<td>
<asp:textbox id="quoteContentTxt" runat="server" TextMode=
"MultiLine" Width="290px" Height="121px">
</asp:textbox>
<asp:RequiredFieldValidator id="quoteContentValidator"
runat="server" ControlToValidate=
"quoteContentTxt" Display="Dynamic"
ErrorMessage="<br />You must supply a
quote!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td valign="top">Your Name:</td>
<td> </td>
<td>
<asp:textbox id="submittedByTxt" runat="server"
TextMode="SingleLine"></asp:textbox>
<asp:RequiredFieldValidator id="submittedByValidator"
runat="server" ControlToValidate="submittedByTxt"
Display="Dynamic" ErrorMessage="<br />
You must supply your name!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> Web Site:</td>
<td> </td>
<td>
<asp:textbox id="websiteTxt" runat="server"
TextMode="SingleLine"></asp:textbox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<asp:button id="addQuoteButton" onclick="addQuote"
runat="server" Text="Add Quote!"></asp:button>
</td>
</tr>
</table>
</form>
</body>
</html>
Now save it as addQuote.aspx, and surf to it. You should see this:

Try it! Cool, eh? Let's go over the code.
The first few lines were the exact same as the previous page: they contained our page directive and imported the namespaces.
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="Quote" %>
We'll skip the actual code right now and jump to the form. As you can see, it's a standard Web Form, using 3 textboxes, 1 button and 2 validators. The validators only check the quoteContentTxt and submttedByTxt to make sure they have some content.
<form name="addQuoteForm" runat="server">
<ASP:Label id="result" CssClass="body" Font-Bold="true"
runat="server"></ASP:Label>
<table style="border: 1 solid #8080C0" class="body">
<tr>
<td valign="top">Quote Content:</td>
<td width="10"> </td>
<td>
<asp:textbox id="quoteContentTxt" runat="server"
TextMode="MultiLine" Width="290px"
Height="121px"></asp:textbox>
<asp:RequiredFieldValidator id="quoteContentValidator"
runat="server" ControlToValidate="quoteContentTxt"
Display="Dynamic" ErrorMessage="<br />You must
supply a quote!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td valign="top">Your Name:</td>
<td> </td>
<td>
<asp:textbox id="submittedByTxt" runat="server"
TextMode="SingleLine"></asp:textbox>
<asp:RequiredFieldValidator id="submittedByValidator"
runat="server" ControlToValidate="submittedByTxt"
Display="Dynamic" ErrorMessage="<br />You must
supply your name!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> Web Site:</td>
<td> </td>
<td>
<asp:textbox id="websiteTxt" runat="server"
TextMode="SingleLine"></asp:textbox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<asp:button id="addQuoteButton"
onclick="addQuote" runat="server"
Text="Add Quote!"></asp:button>
</td>
</tr>
</table>
</form>
When the submit button is clicked, the addQuote() function is fired. It first checks to make sure that the page is valid, the creates an instance of the Quote Web Service. Then it creates a new Boolean that will hold the result of the addQuote() call. If it's retuned true, then the quote was added, and if it's retuned false, then the quote wasn't added.
Sub addQuote(byVal obj As Object, byVal e As EventArgs)
If ( page.isValid) Then
Dim randomQuote As New quote.quote()
Dim addOK As Boolean = randomQuote.AddQuote
(quoteContentTxt.Text, submittedByTxt.Text, websiteTxt.Text)
If ( addOK ) Then
result.Text = "Quote was successfully added!"
quoteContentTxt.Text = ""
submittedByTxt.Text = ""
websiteTxt.Text = ""
Else
result.Text = "Quote was not added!"
End If
End If
End Sub
Finishing Up
Congratulations, you've just made you're first Web Service! While what we covered was very simple, you can always add more functionality to this base, such as returning the number of quotes, or building in the ability to edit or delete quotes. The only limit to what you can do is how much you can be bothered programming!
Web Services are a new and exciting way to open up Websites to the public or your customers! There is no real limit to what you can do with Web Services, and with .NET's platform independence, you can use Web Services that have been generated in any language!
The example we covered was a very simple one, but it should show you want is possible! There are a myriad of resources for Web Services, the most obvious one being the .NET SDK, and Microsoft's newly released Web Enhancements Package for .NET.
This article is part of SitePoint's .NET Feature Guide -- an excellent resource for aspiring and experienced .NET developers. Don't miss it!