Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

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

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

698 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

txtTitle.Text = Request.QueryString.Get(1).ToString();

txtAuthor.Text = Request.QueryString.Get(2).ToString();

}

The preceding code retrieves the QueryString value stored at index 0, 1, and 2 and assigns these values to the txtISBN, txtTitle, and txtAuthor text boxes, respectively.

When the user enters the required details and clicks on the Order button, the

 

 

L

information is added to the underlying database. To do this, add the following

code to the Click event of the btnOrder button.

 

 

F

 

 

M

private void btnOrder_Click(object sender, System.EventArgsY e)

{

A

 

DTService.Service1 srv = new DTService.Service1();

 

E

 

string strDate, strStatus, strOrderBy;

 

strDate = Convert.ToString(DateTime.Today);

 

T

 

strStatus=”Pending”; strOrderBy=”Bookers Paradise”; string result;

result = srv.AcceptDetails(txtISBN.Text, strDate,

txtName.Text, txtAddr1.Text, txtAddr2.Text, txtCity.Text, txtState.Text, strOrderBy, strStatus,

lstCardType.SelectedItem.Text, txtCardNumber.Text );

string orderno;

orderno = srv.GenerateOrder();

if (result == “Record Inserted!!”)

{

string custid;

custid = InsertBookersDB(orderno);

Team-Fly®

DEVELOPING WEB SERVICE CLIENTS

Chapter 30

699

 

 

 

 

TextBox1.Text = “Dear “ + txtName.Text + “!! \n” + “Thanks for visiting Bookers Paradise. \n” +

“Your Customer ID is “ + custid + “.\n” +

“Your order (Number “ + orderno + “) will be shipped by “ + DateTime.Today.AddDays(15).Date + “.”;

}

else

{

TextBox1.Text = “Dear “ + txtName.Text + “!! \n” + “Thanks for visiting Bookers Paradise \n” +

“Your request could not be processed due to some internal error. \n”+ “Please visit later.”;

}

}

The preceding code creates an instance of the DTService.Service1 class. In addition, the code declares three string type variables, strDate, strStatus, and strOrderBy. The strDate variable is initialized to the current date, which is retrieved by the Today property of the System.DateTime struct. However, to store the date in a string type variable, you first need to convert the date type value to a string type value by using the Convert() method. Next, the strStatus variable is initialized to the value Pending and the strOrderBy variable to the value Bookers

Paradise.

Then, a string type variable, result, is declared and initialized to the value returned by the AcceptDetails() Web method.This Web method is used to store the details of the customer and the book, passed as parameters to the Web method, in the DTDetails table. You have learned to write the code for the AcceptDetails() Web method in Chapter 29 in the section “Creating the AcceptDetails() Web Method.”

Next,the code declares and initializes another string type variable, orderno, to the value returned by the GenerateOrder() Web method.This method is used to automatically generate an order number for each order that is placed.

Next, a if construct is used to check whether records are added to the database. If the records are added, a string type variable, custid, is declared. This variable is initialized to a value returned by the InsertBookersDB() method that is used to automatically create the customer ID for all orders that are placed. You will learn to write a code for the InsertBookersDB() method later in this chapter.

700 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

Then, a message is displayed in a text box confirming that the order for a book is successfully placed. Figure 30-10 shows a message displayed to the customer.

FIGURE 30-10 The message displayed to the customer

However, if the AcceptDetails() Web method fails to add the records to the underlying database, an error message is displayed to the customer.

Adding Code to the InsertBookersDB() Method

The InsertBookersDB() method is used to automatically generate the customer ID value for all orders that are placed on the Web site. The order number for the order is passed as the parameter to this method. To create the InsertBookersDB() method, write the following code:

public string InsertBookersDB(string order)

{

string SelStr;

SelStr = “Select Count(*) From BookerCustDetails”;

SqlCommand SelCom;

SelCom = new SqlCommand(SelStr, sqlConnection1); sqlConnection1.Open(); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.Fill(dsCustomers1,”Customer”);

DEVELOPING WEB SERVICE CLIENTS

Chapter 30

 

701

 

 

 

 

 

 

sqlConnection1.Close(); string str;

str = dsCustomers1.Tables[“Customer”].Rows[0][0].ToString (); int val;

val = Convert.ToInt32(str); val= val+1;

if(val>0 & val<=9)

{

str = “C000” + Convert.ToString(val);

}

else if(val>9 & val<=99)

{

str =”C00” + Convert.ToString (val);

}

else if(val>99 & val <=999)

{

str = “C0” + Convert.ToString (val);

}

else

{

str = “C” + Convert.ToString (val);

}

}

In the preceding code, a string type variable SelStr is declared and initialized to a SQL statement that is used to count the records in the BookersCustDetails table. Next, an instance, SelCom, of the SqlCommand class is declared. In the constructor of the SqlCommand class, the variable SelStr is passed as a parameter. In addition, an object of the sqlConnection component is added as a parameter to the constructor of the SqlCommand class.

Once you have assigned the SQL statement to the SelStr variable, the connection to the BookerCustDetails table is opened by using the Open() method. Then, the Fill() method is used to fill the sqlDataAdapter component with the data in the dataset. After the records are added to the sqlDataAdapter component, the connection to the BookerCustDetails table is closed using the Close() method.

The code then declares a string type variable str and initializes it to a collection of rows in the table. To do this, the Rows property of the DataRowCollection class

702 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

is used. The value returned by the Rows property is converted to a string value by using the ToString() method and stored in the str variable. Next, an integer type variable, val, is declared and initialized to the 32-bit signed integer equivalent of the value stored in the str variable. To convert the string type variable to the 32bit signed integer variable, you use the ToInt property of the System.Convert class.

Because the value stored in the variable val is the number of records in the BookerCustDetails table, to generate the next customer ID, you need to add 1 to the value in the variable val. Then, an if construct is used to find the range of the value in the variable val. If this value lies in the range 0 to 9, the string C000 is added to this value. However, to do this, you again need to convert the value in the variable val to a string type value.

Similarly, if the value in the variable val lies in the range 9 to 99, the string C00 is added to the value. Therefore, the range of the value is found out and C followed by zeros is added to make the customer ID a four-digit number. This value stored in the variable str is returned by the method.

Adding Code to Store the Customers’ Details in the Database

As already discussed, the values entered by the user in the Orders form are stored in the database of Deepthoughts Publications. You can write the code that stores the details about the customers in the underlying database. To do this, add the following code to the Orders page:

string InsStr;

InsStr = “Insert Into BookerCustDetails Values(@CID, @CN, @BA1, @BA2, @BC, @BS)”; SqlCommand InsCom;

InsCom = new SqlCommand(InsStr, sqlConnection1); sqlDataAdapter1.InsertCommand = InsCom;

sqlDataAdapter1.InsertCommand.Parameters.Add(“@CID”, SqlDbType.Char,6).Value = str; sqlDataAdapter1.InsertCommand.Parameters.Add(“@CN”, SqlDbType.VarChar,50)

.Value = txtName.Text; sqlDataAdapter1.InsertCommand.Parameters.Add(“@BA1”, SqlDbType.VarChar ,50)

.Value= txtAddr1.Text ; sqlDataAdapter1.InsertCommand.Parameters.Add(“@BA2”, SqlDbType.VarChar,50)

.Value= txtAddr2.Text ; sqlDataAdapter1.InsertCommand.Parameters.Add(“@BC”,SqlDbType.VarChar,20)

.Value = txtCity.Text ;

DEVELOPING WEB SERVICE CLIENTS

Chapter 30

703

 

 

 

 

sqlDataAdapter1.InsertCommand.Parameters.Add(“@BS”, SqlDbType.VarChar ,10)

.Value = txtState.Text ; if(sqlConnection1.State== ConnectionState.Closed )

{

sqlConnection1.Open ();

}

sqlDataAdapter1.InsertCommand.ExecuteNonQuery(); sqlConnection1.Close();

The preceding code declares a string type variable InsStr and stores an SQL query used to insert values to the BookerCustDetails table.The values to be stored are passed to the SQL query. Next, an instance of the SqlCommand class is created to connect to the sqlDataAdapter component. Next, the Add() method is used to add values in the text box controls to the SqlParameterCollection object. To retrieve the value in the text box, the Text property is used.

Next, an if loop is used to check whether the SQL connection is opened or closed. If the connection is closed, you use the Open() method to open the connection. Finally, the ExecuteNonQuery() method of the SqlCommand class is executed to return the records that are affected by the SQL command stored in the SelStr variable. After adding the records, the connection is closed.

Similarly, you can add the code that adds the details of the book for which the user has placed an order in the database of Deepthoughts Publications. You will see the code later in this chapter.

In addition to the Order button, the Orders form contains a Clear button. The following section discusses adding code to the Clear button.

Adding Code to the Clear Button

The Clear button is used to clear all the values entered by the user in the Orders page. To add this functionality, write the following code in the Click event of the

btnClear button:

private void btnClear_Click(object sender, System.EventArgs e)

{

txtISBN.Text=””;

txtTitle.Text =””;

txtAuthor.Text =””;

txtName.Text=””;

704 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

txtAddr1.Text=””;

txtAddr2.Text=””;

txtCity.Text=””;

txtState.Text=””;

TextBox1.Text =””;

txtCardNumber.Text =””;

lstCardType.SelectedIndex =0;

}

The preceding code writes a null value in all the text box controls.

After adding the previously described code snippets to the Orders page, look at the complete code for the OrdersForm form.

using System;

using System.Collections; using System.ComponentModel; using System.Data;

using System.Drawing; using System.Web;

using System.Web.SessionState; using System.Web.UI;

using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient ;

namespace BookersClient

{

public class OrdersForm : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label4; protected System.Web.UI.WebControls.Label Label5; protected System.Web.UI.WebControls.Label Label6; protected System.Web.UI.WebControls.Label Label7; protected System.Web.UI.WebControls.Label Label8; protected System.Web.UI.WebControls.TextBox txtISBN; protected System.Web.UI.WebControls.TextBox txtName;

DEVELOPING WEB SERVICE CLIENTS

Chapter 30

705

 

 

 

 

protected System.Web.UI.WebControls.TextBox txtAddr1; protected System.Web.UI.WebControls.TextBox txtAddr2; protected System.Web.UI.WebControls.TextBox txtCity; protected System.Web.UI.WebControls.RequiredFieldValidator

RequiredFieldValidator3;

protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;

protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;

protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator6;

protected System.Web.UI.WebControls.Button btnOrder; protected System.Web.UI.WebControls.Button btnClear; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Label Label9; protected System.Web.UI.WebControls.Label Label10; protected System.Web.UI.WebControls.Label Label11; protected System.Web.UI.WebControls.Label Label12;

protected System.Web.UI.WebControls.DropDownList lstCardType; protected System.Web.UI.WebControls.TextBox txtCardNumber; protected System.Web.UI.WebControls.TextBox txtTitle; protected System.Web.UI.WebControls.TextBox txtAuthor; protected System.Web.UI.WebControls.RequiredFieldValidator

RequiredFieldValidator1;

protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.HyperLink HyperLink1; protected System.Data.SqlClient.SqlCommand sqlSelectCommand1; protected System.Data.SqlClient.SqlCommand sqlInsertCommand1; protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1; protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1; protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; protected BookersClient.dsCustomers dsCustomers1;

protected System.Web.UI.WebControls.TextBox txtState;

706 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here txtISBN.Text = Request.QueryString.Get(0).ToString(); txtTitle.Text = Request.QueryString.Get(1).ToString(); txtAuthor.Text = Request.QueryString.Get(2).ToString();

}

private void btnOrder_Click(object sender, System.EventArgs e)

{

DTService.Service1 srv = new DTService.Service1(); string strDate, strStatus, strOrderBy;

strDate = Convert.ToString(DateTime.Today); strStatus=”Pending”;

strOrderBy=”Bookers Paradise”; string result;

result = srv.AcceptDetails(txtISBN.Text, strDate,

txtName.Text, txtAddr1.Text, txtAddr2.Text, txtCity.Text, txtState.Text, strOrderBy, strStatus,

lstCardType.SelectedItem.Text, txtCardNumber.Text );

string orderno;

orderno = srv.GenerateOrder();

if (result == “Record Inserted!!”)

{

string custid;

custid = InsertBookersDB(orderno);

DEVELOPING WEB SERVICE CLIENTS

Chapter 30

707

 

 

 

 

TextBox1.Text = “Dear “ + txtName.Text + “!! \n” + “Thanks for visiting Bookers Paradise. \n” +

“Your Customer ID is “ + custid + “.\n” +

“Your order (Number “ + orderno + “) will be shipped by “ + DateTime.Today.AddDays(15).Date + “.”;

}

else

{

TextBox1.Text = “Dear “ + txtName.Text + “!! \n” + “Thanks for visiting Bookers Paradise \n” +

“Your request could not be processed due to some internal error. \n”+ “Please visit later.”;

}

}

public string InsertBookersDB(string order)

{

//Code To Generate Customer ID string SelStr;

SelStr = “Select Count(*) From BookerCustDetails”;

SqlCommand SelCom;

SelCom = new SqlCommand(SelStr, sqlConnection1); sqlConnection1.Open(); sqlDataAdapter1.SelectCommand = SelCom; sqlDataAdapter1.Fill(dsCustomers1,”Customer”); sqlConnection1.Close();

string str;

str = dsCustomers1.Tables[“Customer”].Rows[0][0].ToString (); int val;

val = Convert.ToInt32(str); val= val+1;

if(val>0 & val<=9)

{

str = “C000” + Convert.ToString(val);

}