The .NET platform greatly simplifies the
process of creating and using custom Web services. In this
article, I’ll show you how to develop your own service that
returns data from a backend data source.

The components

The System.Web.Services namespace provides the
necessary classes for creating custom Web services. Specifically, a
Web service is derived from the WebServices class located within
this namespace. In addition, a Web service is a class file that is
created with the asmx file extension. Web service methods are
exposed via the WebMethod attribute, which immediately precedes the
method name. You must declare the method marked with this attribute
as public.

An IDE such as Visual Studio .NET makes it easy
to create a Web service with a few mouse clicks, but it’s just as
easy to use your favorite text editor. I use the latter method in
this newsletter (i.e., I use Microsoft Notepad). A file is declared
as a Web service with the following directive placed at the top of
the file (just like the page director for ASP.NET pages):

<%@ WebService Language=”C#” Class=”ClassName”

The previous line declares the class within the
file as a Web service written in C# using the specific class name.
However, the language choice is up to each developer; you may use
VB.NET, J#, or any other supported language.

Using the details we’ve covered, let’s create a
simple Web service that returns a value from a SQL Server database
(the sample Northwind database).

In action

The next code listing creates a Web service
that returns a numeric value from the Northwind database located on
the SQL Server. This provides a layer of abstraction between the
user and the database connection. The SQL Server connection is
defined within a string constant, which is used to create a
database connection object. A simple SQL select statement is
applied to the database using the SQL SUM function. This returns a
total as a string value. Also, an error is returned if a database
connection problem is encountered with the database operations
contained within a try/catch block.

<%@ WebService Language=”C#”
Class=”BuilderWebServices.BuilderWebServiceExample1″ %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
namespace BuilderWebServices {
public class BuilderWebServiceExample1: WebService {
private const string sConn = “server=(local);Initial
private SqlConnection conn = null;
private SqlCommand comm = null;
public string GetTotalFreight() {
try {
conn = new SqlConnection(sConn);
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = “SELECT SUM(Freight) FROM Orders”;
comm.CommandType = CommandType.Text;
if (comm.ExecuteScalar() == null) {
return “Database error”;
} else {
return comm.ExecuteScalar().ToString();
} } catch (SqlException ex) {
return “Database error: ” + ex.ToString();
} catch (Exception e) {
return e.ToString();
} finally {
if (conn.State == ConnectionState.Open) {
} } } } }

Once you create the Web service file
(BuilderWebServiceExample1.asmx in our example), you may place it
on the Web server and call it directly to obtain its syntax. I
called this sample on a test server using the following URL:


You may obtain the public method’s syntax by
using the following syntax:


You may utilize the method with the following


When you call this method, the following XML
returns this code:

<?xml version=”1.0″ encoding=”utf-8″ ?>

Let’s extend the example by adding a method
that accepts a parameter.

Extending the functionality

Now you’ll add a method that accepts a
parameter and uses this parameter to locate a data element within
the database. It accepts an employee id and locates the associated
employee from the Employees table. Listing A

This method returns the matching employee’s
name if located; otherwise, it returns a message stating that the
employee wasn’t found. If an error occurs, it returns an error
message. Here’s an example of the data returned for a matching

<?xml version=”1.0″ encoding=”utf-8″ ?>
<string xmlns=””>Fuller,

VB.NET equivalent

Listing B includes all the code for
the VB.NET equivalent of the example code in this article. The main
difference is the attribute before the class name declaring it as a
Web service.

Easy to use

The .NET Framework simplifies many tasks, of
which the creation and use of Web services is a prime example. The
Framework makes this complicated Web standard available to all .NET
developers with a few mouse clicks (if you’re using Visual Studio
.NET) or a standard file format via a standard text file.

TechRepublic’s free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!