Creating an ASP.NET Web Service and accessing it from the browser and VB.NET Windows application.
Welcome to the first step-by-step example in this section!
In this example, we'll use Visual Studo.NET beta 2 and .NET Framework SDK Beta 2, to create a ASP.NET Web Serivce (coded using VB.NET) and then access it from a form-based VB.NET Windows application.
Note that these step-by-step examples wil not cover the concepts or the internal details of functioning, but simply practical examples illustrated with the help of screen shots.
I am running Windows 2000 Advanced Server with IIS 5.0 and SQL Server 2000 installed on this same machine. Hence in this example you'll notice that I am connecting to localhost Web and database server and for simplicity I'll use Windows NT Authentication for connecting to SQL Server 2000.
So, let's begin and create a ASP.NET Web Service and a VB.NET Windows client application accessing that Web Service.
Step 1: Select the New Project Type
Start Visual Studio.NET, Select Project Type as Visual Basic Projects and from the right hand side list of templates choose ASP.NET Web Service. Name the project as NWEmployees. As I mentioned I am connecting to "localhost" Web server (IIS 5.0 on my local machine), select this option correctly depending on where your Web server is. And finally click on the OK button.
Step 2: VS.NET connects to the Web server, creates virtual directory and generates the default VB.NET code.
Once you click on the OK button, you'll see following dialog box - this indicates that VS.NET is connecting to the Web server, creating Virtual Directory and the default code.
Step 3: Connecting to SQL Server 2000. Adding Data Connection.
Once VS.NET is done with the above step, you'll see the designer window and your project files. Click on Server Explorer (on the left hand side), right click on Data Connections and choose Add Connection. We'll be connecting to local SQL Server 2000 and to the Northwind database specifically. If you already have a data connection set up, you may skip this step, just make sure you can access Northwind database (that ships with SQL Server 2000 as a sample database).
Step 4: Selecting the SQL Server 2000 and the database.
After clicking on "Add Connection" from the previous step, you'll see the following dialog box. Select the SQL Server, login method and the database. As mentioned earlier, just for the sake of simplicity, I am connecting using NT Authentication, connecting to local server (named HOMEWORK) and we'll work on the Northwind sample database.
Step 5: Drag and Drop the Employees Table and Generate DataSet.
Once the Data Connection is added successfully, expand the data connection tree, expand Tables and drag and drop Employees table on the data designer window. You'll see SqlConnection1 and SqlDataAdapter1 objects on the data designer. Select SQLDataAdapter1, right click on it and choose Generate DataSet.
Step 6: Naming the DataSet.
Once you click on the Generate DataSet menu option, you'll see the following dialog box, change the DataSet name to EmployeeDataSet.
Step 7: Writing the code.
Once the DataSet is generated, switch to the Code View by selecting the "View Code" button under the Solution Explorer. And add the following WebMethod function.
Public Function GetEmployees() As EmployeeDataSet
Dim dsEmployees As New EmployeeDataSet()
The Web Service method is named GetEmployees. It does not take any parameter, but returns a DataSet (EmployeeDataSet) - an object of class that we created in step 6.
Inside the function, we create an instance of EmployeeDataSet, and then pass it as a parameter to SqlDataAdapter's Fill method, and finally we return this object as a return value from the Web Service method GetEmployees().
Step 8: Build and Test the Web Service
Before writing the VB.NET Windows Application to call this Web Service, let's first test it out.
Press Ctrl+F5 to build and execute the Web Service project. Visual Studio generates a default browser based interface where in we can test the Web Service and its methods. After the successful build, you'll see new browser window with the service name, description and its methods. For our Web Service, we have just one method named GetEmployees.
Step 9: The GetEmployees method and sample SOAP request and response payloads.
Click on GetEmployees link and you'll see the following screen, which describes the method and also provides sample SOAP request and response payloads. Click on the Invoke button here.
Step 10: Results of calling a Web Service method.
Clicking on Invokes calls the GetEmployees Web Service method and displays the result XML in a new browser window. Note the resultant XML contains both, schema of the table as well as the data values.
Step 11: Let's now create a VB.NET client application
Congratulations! You just successfully created a Web Service. Now, let's move forward and create a VB.NET Windows application that calls the above Web Service method.
Click File | New | Project, still we'll choose Visual Basic Projects but this time the template from the right hand side should be Windows Application. Select that, name the application as VBEmployees, and click on OK.
Step 12: Adding a Data Grid, etc.
You'll see a Visual Basic Form window. Drag and drop a Button control and a DataGrid control onto the form. Resize the grid accordingly and set it's Anchor property to "Top, Bottom, Left, Right". Place the Button under the DataGrid and select it's Anchor property as "Bottom, Right".
Essentially, at the click of the button, we'll call the Web Service method (GetEmployees) and fill the grid with the DataSet returned by the method call.
In order to call the Web Service method, we'll have to add the reference to the Web Service. Right click on the project and choose Add Web Reference.
Step 13: Providing the Web Service Information for adding the reference.
Type http://localhost/NWEmployees/NWEmployees.vsdisco in the Address box on "Add Web Reference" Dialog box and hit Enter. You'll see the information (Contract and Documentation) about the Web Service that we created above. Simply click on "Add Reference" button here. If your Web server is running on some different machine (or you created Web Service in above steps on some differfent server), update the above URL respectively).
Step 14: Writing the Client Code
Once the Web Service Reference is added, double click on the button and add the following code under Button1_Click method:
Dim EmpWebSvc As New localhost.Service1()
Dim EmpDS As New localhost.EmployeeDataSet()
EmpDS = EmpWebSvc.GetEmployees()
DataGrid1.DataSource = EmpDS
We simply create an instance of Web Service object and call it's GetEmployees method which returns a DataSet. We use this returned DataSet as the DataSource for the DataGrid on the form - which in turn shows the Employees data in the grid.
Step 15: The Results
And here is how the results look like. See how easy it was to create a Web Service and write a client application. The Web Service resided on the local machine in this example but it could have resided on any server on the intranet/internet.