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

DEVELOPING WEB SERVICES |
Chapter 29 |
659 |
|
|
|
|
|
else
{
result = “Record Inserted!!”;
}
return result;
}
[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;
}
[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;
SelStr = “Select * from DTCatalog where ISBNNo = @ISB”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.SelectCommand.Parameters.Add(“@ISB”,SqlDbType.Char, 10)
.Value = ISBN; sqlConnection1.Open();
sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”);

660 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
sqlConnection1.Close();
return dsDetails1;
}
[WebMethod(Description=”This method searches for the details of the book based on the “ + “ the name of the Author”)]
public DataSet SrchAuthor(string Author)
{
string SelStr;
SelStr = “Select * from DTCatalog where Author = @AU”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.SelectCommand.Parameters.Add(“@AU”,SqlDbType.
VarChar , 50).Value = Author; sqlConnection1.Open(); sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();
return dsDetails1;
}
[WebMethod(Description=”This method searches for the details of the book based on the “ +” the Catalog of the books”)]
public DataSet SrchCategory(string Catalog)
{
string SelStr;
SelStr = “Select * from DTCatalog where Category = @CA”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.SelectCommand.Parameters.Add(“@CA”,SqlDbType.Char , 10)
.Value = Catalog; sqlConnection1.Open();
sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();

DEVELOPING WEB SERVICES |
Chapter 29 |
661 |
|
|
|
|
|
return dsDetails1;
}
[WebMethod(Description=”This method searches for the details of the book based on the “ + “ the Title of the books”)]
public DataSet SrchTitle(string BkTitle)
{
string SelStr;
SelStr = “Select * from DTCatalog where BookTitle = @BT”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.SelectCommand.Parameters.Add(“@BT”,SqlDbType.VarChar , 50)
.Value = BkTitle; sqlConnection1.Open();
sqlDataAdapter1.SelectCommand.ExecuteNonQuery(); sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();
return dsDetails1;
}
[WebMethod(Description=”This method returns the order number of a customer”)] public string GenerateOrder()
{
string SelStr;
SelStr = “Select Count(*) From DTOrders”;
SqlCommand SelCom;
SelCom = new SqlCommand(SelStr, sqlConnection1); sqlConnection1.Open(); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.Fill(dsDetails1,”Details”); sqlConnection1.Close();
string str;
str = dsDetails1.Tables[“Details”].Rows[0][0].ToString (); int val;
val = Convert.ToInt32(str);

662 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
val= val+1;
if(val>0 & val<=9)
{
str = “O000” + Convert.ToString(val);
}
else if(val>9 & val<=99)
{
str =”O00” + Convert.ToString (val);
}
else if(val>99 & val <=999)
{
str = “O0” + Convert.ToString (val);
}
else
{
str = “O” + Convert.ToString (val);
}
return str;
}
}
}
After creating the Web service, you can test the Web service.
Testing the Web Service
To test the Web service, press the F5 key or select the Start option on the Debug menu. Because you have tested most of the Web methods while creating them, you can test the remainder of the Web methods.
Testing the SrchAuthor() Web Method
On testing the SrchAuthor() Web method, the method returns records for the specified author. Figure 29-9 shows the records returned by the SrchAuthor() Web method.


664 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
Testing the SrchTitle() Web Method
When the user wants to search for a particular book, the user can specify the title of the book as the search criteria. Figure 29-11 shows the record returned by the
SrchTitle() Web method.
FIGURE 29-11 The record returned by the SrchTitle() Web method
Once you have created a Web service, you need to secure your Web service. The following section discusses how to secure a Web service.
Securing a Web Service
It is essential that you secure the Web service that you create.This would prevent anyone else from tampering with your Web service. To secure a Web service, there are several attributes associated with the Web service, as shown:
Authentication
Authorization
Auditing

DEVELOPING WEB SERVICES |
Chapter 29 |
665 |
|
|
|
|
|
Data integrity
Data privacy
Data availability
Among all these attributes, authentication is the most important attribute. To provide security to your Web service, you need to have a secure mechanism for authentication. Authentication is defined as the process of verifying the details of the user attempting to access the Web service. This verification is done on the basis of the information stored about the user. This information may include a password, an ID, or a thumbprint. These credentials stored for a user are called principal. However, to avoid a situation in which an unauthorized user tries to access the Web service by using the password assigned to an authorized user, you need to carefully decide the authentication credentials for your Web service.
Summary
In this chapter, you learned how to create the DTWebService Web service. While creating the Web service, you added the required Web methods to the Web service. These Web methods include AcceptDetails(), GenerateOrder(),
SearchALL(), SrchISBN(), SrchTitle(), SrchCategory(), and SrchAuthor(). In this
way, you can also create a Web service for Black and White Publications.
After adding the Web methods to the DTWebService Web service, you tested the Web service in the Internet Explorer window. Finally, you learned to secure a Web service.
This page intentionally left blank
