6. Click OK. The intel_wsNWProducts project is created and added to a Solution by the same name. Service1.asmx is open in design view.
7. Click the design surface and press F4 to access the Web service page's Properties window.
8. Set the Name property of Service1 to ProductsService.
9. In Solution Explorer, right-click the Service1.asmx file, select Rename, and rename the file Products.asmx to match the service name.
Exposing a Web method
Functions and subroutines that you want to expose to the public over HTTP are essentially no different than non-Web service methods. The only difference is the use of the <WebMethod> attribute, as you will see.
The method you will build for this article's Web service allows anyone to retrieve a list of products from the Northwind database by product category. The user types in a CategoryID, which is forwarded to the Web service. The Web service returns the products in that category. (If no category is entered by the user, all products are returned.)
1. In the Code Editor window right-click on Products.asmx.vb and select View Code.
2. Replace the commented lines with the following shell of a function that returns a DataSet:
Public Function GetProducts(ByVal strCategoryID As Int32) As DataSet End Function |
In the next step you will add an Imports statement that lets you use shorthand notation when accessing classes in the imported namespace. A namespace is simply a logical grouping of related .NET types, such as classes, interfaces, enums, structures, and so forth. This makes it easier to work with the Base Class Library and also prevents type name conflicts.
You can always access the type using a fully qualified name like this: System.Data.SqlClient.SqlConnection. The advantage of an Imports statement, however, is that you can simplify and lighten your code with the use of shorthand dot notation. Thus, if you import the System.Data.SqlClient namespace, in your code you need only type SqlConnection.
3. At the top of the page of code add the following:
Imports System.Data.SqlClient |
Classes in the SqlClient namespace classes are optimized for use with SQL Server. If you were working with another database, you could use classes in the System.Data.OleDb.
4. Create a SqlConnection object. Add the following to the function (adjust the connection string for your setup):
Dim cn As New
SqlConnection("server=(localhost);uid=sa;pwd=;database=northwind")
|
5. Create a SqlCommand object. This object executes SQL statements against a SQL Server database. You are passing in two arguments, the SQL statement that, at this point, returns all of the products, as well as the newly created Connection object. Add this code:
6. To support filtering the products by CategoryID, add the next few lines of code. The SQL has been built this way so that if the user doesn't enter a CategoryID, the Web service assumes that he or she wants to view all products:
![]()
If you're interested in this topic, these articles may be helpful:
![]() | Building web services with Visual Studio .NET Understanding how web services work is key to using Visual Studio .NET... |
![]() | ASP.NET 2.0 Demystified, Chapter 4: Variables and Expressions in ASP.NET by Jim Keogh McGraw-Hill/Osborne Books ASP .NET 2.0 Demystified e... |
![]() | Security for ASP.NET Developers Increasingly, ASP.NET developers are being asked to wear two hats; one... |
![]() | .NET client application "no touch" deployment: harness the full processing power of the desktop by Christopher Goldfarb, senior software architect, Intel Software Sol... |
![]() | Beyond Software Architecture: Creating and Sustaining Winning Solutions, Chapter 8 - Integration and Extension by Luke Hohmann. Publisher: Addison-Wesley and Prentice Hall. As de... |
![]()
Related Jobs:


