ūü§Į New: Coding Assessments Practice your skills on real-world programming challenges

Creating and Consuming .NET Web Services in 5 Easy Steps

Dimitrios Markatos

Step 3 ‚Äď Build our Object

OK, now let’s invoke the .NET C# compiler (csc.exe) or VB compiler (vbc.exe) to convert your source file into an assembly or DLL. Create a new .bat file named makelib.bat and type in:


csc /t:library /out:binGetSuppliers.dll binGetSuppliers.cs      
 System.Web.Services.dll,System.XML.dll /optimize


vbc /t:library /out:binGetSuppliers.dll binGetSuppliers.vb      
 System.Web.Services.dll,System.XML.dll /optimize    

All this does is compile the source file from the bin folder to a DLL of set name, to the bin folder.

The /t:library instructs the compiler to create a dll (dynamic link library), rather than an exe (executable) file, with /reference: importing the necessary dll’s libraries that will be used in our Web service. Finally, we /optimize our dll to produce smaller, faster, and more efficient output.

Step 4 ‚Äď Put it All Together

Now copy and paste the code below to an .aspx .NET page and name it. This code is for the page that will access the assembly/DLL in your bin folder, and with all the Web server controls, pass the appropriate parameters to the Web Service. Go ahead and run it (http://localhost/Websrvce.aspx).

<%@ Page Language="C#" Explicit="true" Strict="true" Buffer="true"%>   
<%@ Import Namespace="System.Data" %>  
<%@ Import Namespace="System.Data.SqlClient" %>  
<%@ Import Namespace="WService" %>  
<script language="C#" runat="server">  
void Page_Load(Object sender, EventArgs e)    
void SubmitBtn_Click (object src, EventArgs e)  
 int RcdCount;  
 string Catg = DropDown1.SelectedItem.Text;  
 //Instantiate DLL  
 GetInfo supInfo = new GetInfo();  
 //Pass parameter into DLL function  
 DataSet MyData = supInfo.ShowSuppliers(Catg);  
 MyDataGrid.DataSource = MyData.Tables[0].DefaultView;  
 RcdCount = MyData.Tables[0].Rows.Count;  
 if (RcdCount <= 0)    
 Message.InnerHtml = "<b>No results were found for <FONT    
                    Color=Red><i>"+ Catg +"</i></font>";  
 MyDataGrid.Visible = false; //Hide Results until needed  
   Message.InnerHtml = "<b><FONT Color=Red><i>" + Catg +  
    "</i></font> has " + RcdCount + " local suppliers</b>";  
 MyDataGrid.Visible = true;  
<body style="font: 10pt verdana">  
<h4>Accessing Data with Web Services</h4>  
<form runat="server">  
<asp:DropDownList id=DropDown1 runat="server">  
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/>  
<span id="Message" runat="server"/>  
<ASP:DataGrid id="MyDataGrid" runat="server"  

How It Works

Pretty cool, huh? Now, aside from the common data accessing and Web server control usage, I’ll outline some specifics. By virtue of creating our proxy class with a namespace in Step 2, we need to import it to the dll we created in Step 3, like this:

<%@ Import Namespace="WService" %>

We then instantiate it or reference our object:

GetInfo supInfo = new GetInfo();


Dim supInfo As New WService.GetInfo()

This is our Web Service class. We then retrieve our dropdown list parameter, and pass it to our ShowSuppliers constructor method like so:

string Catg = DropDown1.SelectedItem.Text;  
DataSet MyData = supInfo.ShowSuppliers(Catg);


Dim Catg As String = DropDown1.SelectedItem.Text   
Dim MyData As DataSet = supInfo.ShowSuppliers (Catg)

And the rest? Just common .NET stuff, which I assume you have some grip on. Nevertheless, it shouldn’t be too hard to follow or figure out. The .NET documentation and Quickstart Tutorials will help with plenty more information.
Well, guess what? We’re done!

If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Introductory Web Development Using ASP.NET.

Go to page: 1 | 2 | 3 | 4