
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf


640 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
Summary
In this chapter, you learned about distributed applications. Distributed applications are scalable applications in which data is shared across applications. Therefore, distributed applications include applications built on different platforms or by using different programming languages. To create a large-scale business solution, it is essential that you integrate these applications. Integration of applications built on various platforms is made simpler with the use of Web services.
A Web service is a reusable component, such as a method, that can be used by any Web application running on the Internet. These applications are called Web service client applications. An application that hosts the Web service is called a Web service provider application.
Next, you learned about the architecture of a Web service.The Web service architecture includes a four-layered model. These layers are the data layer, the data access layer, the business layer, and the listener layer. Based on this architecture, you learned about the working of the Web service.
Next, you learned about the role of XML, WSDL, SOAP, and UDDI in a Web service.Based on this knowledge about a Web service, you learned to create a simple Web service using Visual Studio .NET.


642 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
In the preceding chapters, you looked at the case study and design of the Web site of Bookers Paradise. In addition, you were introduced to the basics of an ASP.NET Web service. Based on the knowledge about Web services, you learned to create a sample Web service by using Visual Studio .NET. This chapter dis-
cusses how to create a Web service for Deepthoughts Publications.
Creating a Web Service for
Deepthoughts Publications
A Web service for Deepthoughts Publications provides information about books published at its publishing house. This information is displayed on the Web site of Bookers Paradise. In addition to searching for information about all the books published by Deepthoughts Publications, a user can search for selected books based on criteria.
To start with, you can create a Web service by using the ASP.NET Web Service template provided by Visual Studio .NET. Refer to this Web service as DTWebService. To create the Web service, perform the following steps:
1.On the File menu, point to the New option.
2.In the displayed list, select the Project option. The New Project dialog box is displayed.
3.In the Project Types: pane, select the Visual C# Projects option.
4.In the Templates: pane, select the ASP.NET Web Service option.
5.In the Location: text box, type the name of the Web service as
DTWebService.
6. Click on the OK button to create the DTWebService Web service.
Figure 29-1 shows the New Project dialog box for DT WebService.

DEVELOPING WEB SERVICES |
Chapter 29 |
643 |
|
|
|
|
|
FIGURE 29-1 The New Project dialog box for DTWebService
Visual Studio .NET creates the Web service and the default files in the Web service. I have discussed the default files generated by Visual Studio .NET for a Web service in Chapter 28, “Exploring ASP.NET Web Services,” in the section “Web Services in the .NET Framework.”
After creating the Web service, add a short description of the Web service. This will help any user looking for a similar Web service. To write a short description, add the following code at the beginning of the Web service.
[WebService (Namespace=”http://LocalHost/DTWebService/”, Description=”A service
displaying catalogs of Deepthoughts publisher”)]
Because the Web service that you create needs to connect to a database, you need to create a data connection object. To create a data connection object to connect to a database, perform the following steps:
1.In the Server Explorer window, expand the Servers node.
If the Ser ver Explorer window is not displayed, you can select the Server Explorer option on the View menu.
When you expand the Servers node, a list of the available SQL servers is displayed.

644 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
2.Browse for the DTDB database and drag the DTCatalog table to the Service1.asmx page.
Visual Studio .NET automatically adds sqlConnection and sqlDataAdapter components to the Service1.asmx page. When the sqlConnection and sqlDataAdapter components are added, you need to create a dataset that accesses data from the DTDB database. To do this, perform the following steps:
1.On the Data menu, select the Generate Dataset option. The Generate Dataset dialog box is displayed.
2.In the Choose a dataset: group box, select the New radio button.
3.Specify the name of the dataset as dsDetails1.
4.Check the Add this dataset to the designer window check box.
5.Click on the OK button to close the Generate Dataset dialog box.
The dataset with the name dsDetails1 is added to the Service1.asmx page. Figure 29-2 shows the sqlConnection, sqlDataAdapter, and dataset components added to the Service1.asmx page.
FIGURE 29-2 The Service1.asmx page with the components added
Once a Web service is created and the components are added, you can add Web methods to it. Web methods are required to provide the functionality to the Web site. The following section discusses the Web methods in the Web service of Deepthoughts Publications.

DEVELOPING WEB SERVICES |
Chapter 29 |
645 |
|
|
|
|
|
Creating the SearchAll() Web Method
Creating a Web service involves coding for the Web methods in the Web service. In this case, you need to create a Web method that returns information about all the books published at Deepthoughts Publications. Before creating the Web method, add a short description about the Web method.
[WebMethod(Description=”This method searches for the details of all books published
by Deepthoughts Publications”)]
Now add the code for the Web method. The code for the Web method is as displayed:
[WebMethod(Description=”This method searches for the details of all books published by Deepthoughts Publications”)]
public DataSet SearchALL()
{
string SelStr;
SelStr = “Select * from DTCatalog”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlConnection1.Open(); sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();
return dsDetails1;
}
The preceding code declares a string variable with the name SelStr. Next, the code initializes the SelStr variable to a SQL statement that retrieves all the records from the DTCatalog table. Next, an instance of the SqlCommand class is created with the name SelCom. The SqlCommand class is present in the System.Data.SqlClient namespace. The SqlCommand class is used to represent a SQL statement that is executed against a SQL server database. You can derive a class from a SqlCommand class.
After declaring the SelCom instance, you can initialize it. In the constructor of the SqlCommand class, you need to pass two parameters, SelStr and sqlConnection1. Because the SqlCommand class represents all the SQL statements executed against a SQL ser ver database, you need to specify the SQL statement by passing it as a


DEVELOPING WEB SERVICES |
Chapter 29 |
647 |
|
|
|
|
|
The Service1.asmx page contains a link to the SearchAll() method. Click on the link to view the Service1.asmx page for the SearchAll() method. This page contains an Invoke button. When the user clicks on the Invoke button, the SearchAll() method is executed and the results are displayed as shown in Figure 29-4.
FIGURE 29-4 The results returned by the SearchAll() method
Creating the SrchISBN() Web Method
You can now create a Web method that returns the records from the DTCatalog table with the ISBN number that is passed as a parameter to the Web method. First, write a description for the Web method.
[WebMethod(Description=”This method searches for the details of the book based on
the “ + “ ISBN Number of the book”)]
After writing the description, you need to write the code for the Web method SrchISBN(). The code for the Web method is as follows:
[WebMethod(Description=”This method searches for the details of the book based on the “ + “ ISBN Number of the book”)]
public DataSet SrchISBN(string ISBN)
{
string SelStr;