Creating the Online Book Store Web application


To create the Online Book Store Web application, you need to create the Login, Book Store, View Cart, Place Order, Order, and Customer Registration Web pages.

Creating the Login Web Page

The Login Web page enables an end user to specify login credentials and log on to access the Book Store Web page. In addition, using the Login Web page, a new end user can register and obtain a user name and password.

Listing 3-10 shows the design for the Login.aspx file:

Listing 3-10: GUI for the Login Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="Login.aspx.vb" Inherits="OnlineBookStore.Login" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Login</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana" MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:textbox id="txtPassword" style="Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 128px" tabIndex="2" runat="server" TextMode="Password" Height="24px" Width="160px"> </asp:textbox> <asp:imagebutton id="cmdRegister" style="Z-INDEX: 108; LEFT: 160px; POSITION: absolute; TOP: 288px" tabIndex="4" runat="server" Width="144px" ImageUrl="file:///D:\ss\testUMLApplication\Images\Register.JPG"> </asp:imagebutton> <asp:label id="lblMessage" style="Z-INDEX: 107; LEFT: 216px; POSITION: absolute; TOP: 192px" runat="server" Height="40px" Width="304px" Visible="False" BackColor="Transparent"> </asp:label> <asp:imagebutton id="cmdLogin" style="Z-INDEX: 106; LEFT: 304px; POSITION: absolute; TOP: 160px" tabIndex="3" runat="server" ImageUrl=".\Images\Login.JPG" BackColor="Transparent"> </asp:imagebutton> <asp:textbox id="txtUserName" style="Z-INDEX: 105; LEFT: 304px; POSITION: absolute; TOP: 96px" tabIndex="1" runat="server" Height="24" Width="160px" EnableViewState="False"> </asp:textbox> <asp:label id="lblPassword" style="Z-INDEX: 102; LEFT: 216px; POSITION: absolute; TOP: 136px" runat="server" Height="16" Width="81" BackColor="Transparent">Password: </asp:label>&nbsp; <asp:label id="lblUserName" style="Z- INDEX: 103; LEFT: 216px; POSITION: absolute; TOP: 104px" runat="server" Height="16px" Width="81px" BackColor="Transparent">User name: </asp:label> <asp:panel id="loginPanel" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 56px" runat="server" Height="184px" Width="440px" ForeColor="Transparent" BackImageUrl=".\Images\LoginBackGroundImage.JPG"> </asp:panel> <asp:label id="lblRegister" style="Z-INDEX: 109; LEFT: 160px; POSITION: absolute; TOP: 256px" runat="server" Height="24px" Width="320px" BackColor="White" Font-Size="Larger">New User Register Here </asp:label>  </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the Login Web page.

Listing 3-11 shows the code for the Login.aspx file:

Listing 3-11: The Login.aspx File
start example
 Imports System.Data.SqlClient Public Class Login    Inherits System.Web.UI.Page    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox    Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label    Protected WithEvents lblPassword As System.Web.UI.WebControls.Label    Protected WithEvents lblUserName As System.Web.UI.WebControls.Label    Protected WithEvents cmdRegister As System.Web.UI.WebControls.ImageButton    Protected WithEvents cmdLogin As System.Web.UI.WebControls.ImageButton    Protected WithEvents loginPanel As System.Web.UI.WebControls.Panel    Dim sqlCommand As SqlCommand    Dim sqlda As SqlDataAdapter    Protected WithEvents lblRegister As System.Web.UI.WebControls.Label    Dim ds As DataSet    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        txtUserName.Attributes.Add("AUTOCOMPLETE", "Off")       'Refresh the login information in the session.       Session("UserName") = ""       Session("Password") = ""       Session("CustomerId") = ""       Session.Remove("BookCart")    End Sub    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As _    System.Web.UI.ImageClickEventArgs) Handles cmdLogin.Click       If txtPassword.Text = "" And txtUserName.Text = "" Then          lblMessage.Visible = True          lblMessage.Text = "Please enter user name and password."          Exit Sub          End If          'Validate the login information that the end user specifies in the user name and password          textboxes       Try          sqlCommand = New SqlCommand("select CustomerId from customers where username='" &          txtUserName.Text & "' and password='" & txtPassword.Text & "'",          Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)          ds = New DataSet          sqlda.Fill(ds)          Catch ex As Exception          lblMessage.Visible = True          lblMessage.Text = "Error validating user name and password."          Exit Sub       End Try       'If the login is performed successfully, store the login information in the session.       If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then          Session("UserName") = txtUserName.Text          Session("Password") = txtPassword.Text          Session("CustomerId") = ds.Tables(0).Rows(0).Item(0)          lblMessage.Visible = False          Else          lblMessage.Visible = True          lblMessage.Text = "Please check the user name and password."          Exit Sub       End If       'Go to the book store page       Response.Redirect("BookStore.aspx")    End Sub    Private Sub cmdRegister_Click(ByVal sender As System.Object, ByVal e As _    System.Web.UI.ImageClickEventArgs) Handles cmdRegister.Click       'Clear the session state and go to customer registration page.       Session("UserName") = ""       Session("Password") = ""       Session("CustomerId") = ""       If Not Session("BookCart") Is Nothing Then          Session("BookCart") = ""       End If       Response.Redirect("CustomerInfo.aspx")    End Sub End Class 
end example
 

Download this Listing .

The above listing defines the following methods :

  • cmdLogin_Click() : Executes when an end user specifies the login credentials and clicks the Login button. This method validates login information and if the information is valid, this method retrieves the Customer Id associated with a specified user name. This method defines the CustomerId session variable that contains the Customer Id, which this method retrieves from the Customers table and the UserName session variable that contains the user name, which the end user specifies in the User name text box. This method then loads the Book Store Web page.

  • cmdRegsiter_Click() : Executes when an end user clicks the Register button to obtain a user name and password. This method invokes the Customer Registration Web page.

Creating the Customer Registration Web Page

The Customer Registration Web page enables an end user to specify customer- related information required to access the Online Book Store Web application.

Listing 3-12 shows the design for the CustomerInfo.aspx file:

Listing 3-12: GUI for the Customer Registration Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="CustomerInfo.aspx.vb" Inherits="OnlineBookStore.CustomerInfo" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Customer Registration</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana" MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:label id="lblFirstName" style="Z-INDEX: 100; LEFT: 24px; POSITION: absolute; TOP: 176px" runat="server" Width="64px">First Name </asp:label> <asp:Label id="lblMandatoryInfo2" style="Z-INDEX: 139; LEFT: 144px; POSITION: absolute; TOP: 544px" runat="server" Width="112px" Height="20px">are mandatory </asp:Label> <asp:label id="lblMandatory1" style="Z-INDEX: 138; LEFT: 128px; POSITION: absolute; TOP: 544px" runat="server" Width="24px" ForeColor="Red">* </asp:label> <asp:label id="lblFormInfo" style="Z-INDEX: 135; LEFT: 256px; POSITION: absolute; TOP: 16px" runat="server" Width="328px" ForeColor="White" Height="8px" BackColor="Blue" Font-Size="Medium">CUSTOMER REGISTRATION FORM </asp:label> <asp:label id="lblMandatory6" style="Z-INDEX: 134; LEFT: 456px; POSITION: absolute; TOP: 264px" runat="server" Width="24px" ForeColor="Red">* </asp:label> <asp:textbox id="txtAddress2" style="Z-INDEX: 130; LEFT: 144px; POSITION: absolute; TOP: 312px" tabIndex="6" runat="server" Width="305" Height="55"> </asp:textbox> <asp:textbox id="txtAddress1" style="Z-INDEX: 129; LEFT: 144px; POSITION: absolute; TOP: 240px" tabIndex="5" runat="server" Width="305px" Height="55px"> </asp:textbox> <asp:label id="lblMandatory5" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 184px" runat="server" Width="24px" ForeColor="Red">* </asp:label> <asp:textbox id="txtCity" style="Z-INDEX: 128; LEFT: 144px; POSITION: absolute; TOP: 376px" tabIndex="7" runat="server"> </asp:textbox> <asp:textbox id="txtZipCode" style="Z-INDEX: 127; LEFT: 144px; POSITION: absolute; TOP: 408px" tabIndex="8" runat="server" MaxLength="5"> </asp:textbox> <asp:textbox id="txtCountry" style="Z-INDEX: 126; LEFT: 144px; POSITION: absolute; TOP: 440px" tabIndex="9" runat="server"> </asp:textbox> <asp:label id="lblMessage" style="Z-INDEX: 125; LEFT: 24px; POSITION: absolute; TOP: 48px" runat="server" Width="680px" Height="24px" Visible="False"> </asp:label> <asp:imagebutton id="cmdSubmit" style="Z-INDEX: 124; LEFT: 24px; POSITION: absolute; TOP: 576px" tabIndex="12" runat="server" Width="120px" ImageUrl=".\Images\Submit.JPG"> </asp:imagebutton> <asp:label id="lblReTypePassword" style="Z-INDEX: 123; LEFT: 24px; POSITION: absolute; TOP: 144px" runat="server" Width="113px">Retype password </asp:label> <asp:label id="lblMandatory3" style="Z-INDEX: 122; LEFT: 304px; POSITION: absolute; TOP: 120px" runat="server" Width="113px" ForeColor="Red">* </asp:label> <asp:textbox id="txtFirstName" style="Z-INDEX: 102; LEFT: 144px; POSITION: absolute; TOP: 176px" runat="server" tabIndex="3"> </asp:textbox> <asp:label id="lblPassword" style="Z-INDEX: 120; LEFT: 24px; POSITION: absolute; TOP: 112px" runat="server" Width="64px">Password </asp:label> <asp:label id="lblMandatory2" style="Z-INDEX: 121; LEFT: 304px; POSITION: absolute; TOP: 88px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:textbox id="txtPassword2" style="Z-INDEX: 103; LEFT: 144px; POSITION: absolute; TOP: 144px" tabIndex="2" runat="server" TextMode="Password"> </asp:textbox> <asp:label id="lblUserName" style="Z-INDEX: 119; LEFT: 24px; POSITION: absolute; TOP: 80px" runat="server" Width="64px">User name </asp:label> <asp:textbox id="txtUsername" style="Z-INDEX: 105; LEFT: 144px; POSITION: absolute; TOP: 80px" runat="server"> </asp:textbox> <asp:label id="lblPhone" style="Z-INDEX: 118; LEFT: 24px; POSITION: absolute; TOP: 512px" runat="server" Width="64px">Phone </asp:label> <asp:textbox id="txtPhone" style="Z-INDEX: 108; LEFT: 144px; POSITION: absolute; TOP: 504px" tabIndex="11" runat="server"> </asp:textbox> <asp:label id="lblEmail" style="Z-INDEX: 117; LEFT: 24px; POSITION: absolute; TOP: 480px" runat="server" Width="64px">Email </asp:label> <asp:label id="lblMandatory9" style="Z-INDEX: 116; LEFT: 304px; POSITION: absolute; TOP: 448px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:textbox id="txtEMail" style="Z-INDEX: 107; LEFT: 144px; POSITION: absolute; TOP: 472px" tabIndex="10" runat="server"> </asp:textbox> <asp:label id="lblCountry" style="Z-INDEX: 115; LEFT: 24px; POSITION: absolute; TOP: 448px" runat="server" Width="64px">Country </asp:label> <asp:label id="lblMandatory8" style="Z-INDEX: 114; LEFT: 304px; POSITION: absolute; TOP: 416px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:textbox id="txtPassword" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute; TOP: 112px" tabIndex="1" runat="server" TextMode="Password"> </asp:textbox> <asp:label id="lblZipCode" style="Z-INDEX: 112; LEFT: 24px; POSITION: absolute; TOP: 416px" runat="server" Width="89px">Zip Code </asp:label> <asp:label id="lblMandatory4" style="Z-INDEX: 113; LEFT: 304px; POSITION: absolute; TOP: 152px" runat="server" Width="89px" ForeColor="Red">* </asp:label> <asp:label id="lblMandatory10" style="Z-INDEX: 111; LEFT: 304px; POSITION: absolute; TOP: 480px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:textbox id="txtLastName" style="Z-INDEX: 106; LEFT: 144px; POSITION: absolute; TOP: 208px" tabIndex="4" runat="server"> </asp:textbox> <asp:label id="lblCity" style="Z-INDEX: 109; LEFT: 24px; POSITION: absolute; TOP: 384px" runat="server" Width="64px">City </asp:label> <asp:label id="lblMandatory7" style="Z-INDEX: 110; LEFT: 304px; POSITION: absolute; TOP: 384px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:label id="lblAddress1" style="Z-INDEX: 131; LEFT: 24px; POSITION: absolute; TOP: 240px" runat="server" Width="64px">Address1 </asp:label> <asp:label id="lblLastName" style="Z-INDEX: 132; LEFT: 24px; POSITION: absolute; TOP: 208px" runat="server">Last Name </asp:label> <asp:label id="lblAddress2" style="Z-INDEX: 133; LEFT: 24px; POSITION: absolute; TOP: 312px" runat="server">Address2 </asp:label> <asp:Label id="lblMandatoryInfo1" style="Z-INDEX: 136; LEFT: 24px; POSITION: absolute; TOP: 544px" runat="server" Width="112px" Height="20px">Fields marked as </asp:Label> </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the Customer Registration Web page.

Listing 3-13 shows the code for the CustomerInfo.aspx file:

Listing 3-13: The CustomerInfo.aspx File
start example
 Imports System.Data.SqlClient Public Class CustomerInfo    Inherits System.Web.UI.Page    Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox    Protected WithEvents txtEMail As System.Web.UI.WebControls.TextBox    Protected WithEvents txtPhone As System.Web.UI.WebControls.TextBox    Protected WithEvents txtPassword2 As System.Web.UI.WebControls.TextBox    Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox    Protected WithEvents txtCity As System.Web.UI.WebControls.TextBox    Protected WithEvents txtCountry As System.Web.UI.WebControls.TextBox    Protected WithEvents Label27 As System.Web.UI.WebControls.Label    Protected WithEvents txtAddress1 As System.Web.UI.WebControls.TextBox    Protected WithEvents txtAddress2 As System.Web.UI.WebControls.TextBox    Protected WithEvents txtZipCode As System.Web.UI.WebControls.TextBox    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label    Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox    Protected WithEvents cmdSubmit As System.Web.UI.WebControls.ImageButton    Dim sqlCommand As SqlCommand    Dim sqlda As SqlDataAdapter    Protected WithEvents lblFirstName As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatoryInfo2 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory1 As System.Web.UI.WebControls.Label    Protected WithEvents lblFormInfo As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory6 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory5 As System.Web.UI.WebControls.Label    Protected WithEvents lblReTypePassword As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory3 As System.Web.UI.WebControls.Label    Protected WithEvents lblPassword As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory2 As System.Web.UI.WebControls.Label    Protected WithEvents lblUserName As System.Web.UI.WebControls.Label    Protected WithEvents lblPhone As System.Web.UI.WebControls.Label    Protected WithEvents lblEmail As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory9 As System.Web.UI.WebControls.Label    Protected WithEvents lblCountry As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory8 As System.Web.UI.WebControls.Label    Protected WithEvents lblZipCode As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory4 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory10 As System.Web.UI.WebControls.Label    Protected WithEvents lblCity As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory7 As System.Web.UI.WebControls.Label    Protected WithEvents lblAddress1 As System.Web.UI.WebControls.Label    Protected WithEvents lblLastName As System.Web.UI.WebControls.Label    Protected WithEvents lblAddress2 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatoryInfo1 As System.Web.UI.WebControls.Label    Dim ds As DataSet    Private Sub cmdsubmit_Click(ByVal sender As System.Object, ByVal e As    System.Web.UI.ImageClickEventArgs) Handles cmdSubmit.Click    'Checking for the mandatory fields     If txtUsername.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify User name."        Exit Sub     End If     If txtPassword.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify Password."        Exit Sub     End If     If txtPassword2.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please retype Password."        Exit Sub     End If     If txtAddress1.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify Address1."        Exit Sub     End If     If txtCity.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify City."        Exit Sub     End If     If txtZipCode.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify Zip code."        Exit Sub     End If     If txtCountry.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify Country."        Exit Sub     End If     If txtEMail.Text = "" Then        lblMessage.Visible = True       lblMessage.Text = "Please specify Email."        Exit Sub     End If    'If the password and confirmation password are not same, display a message.       If txtPassword.Text <> txtPassword2.Text Then        lblMessage.Visible = True       lblMessage.Text = "Password and confirmation password should be same."        Exit Sub     End If    'Validate the Email specified by the customer    If txtEMail.Text.IndexOf(".") < txtEMail.Text.IndexOf("@") Then        lblMessage.Visible = True       lblMessage.Text = "Invalid E-mail format."        Exit Sub     End If     lblMessage.Visible = False    'This variable contains the Id of the newly added customer.     Dim intNewCustomerID As Integer    'Retrieve total number of customers in Customers table     Try       sqlCommand = New SqlCommand("select count(customerId) from Customers",       Application("objSqlConnection"))       sqlda = New SqlDataAdapter(sqlCommand)        ds = New DataSet        sqlda.Fill(ds)        Catch ex As Exception        lblMessage.Visible = True       lblMessage.Text = "Error in saving the customer information"        Exit Sub     End Try     'Add the new customer in the database    If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then       intNewCustomerID = CInt(ds.Tables(0).Rows(0).Item(0)) + 1       'Check whether the specified user name exists for any customer        Try          sqlCommand = New SqlCommand("select customerId from Customers where username='" &          txtUsername.Text & "'", Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)           ds = New DataSet           sqlda.Fill(ds)           Catch ex As Exception        End Try       If Not ds.Tables.Count = 0 And ds.Tables(0).Rows.Count = 0 Then          'The user name does not exists. Add the new customer information into database           Try             sqlCommand = New SqlCommand("insert into Customers values(" & intNewCustomerID & ",'"             & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtAddress1.Text             & "','" & txtAddress2.Text & "','" & txtCity.Text & "','" &             txtZipCode.Text & "','" & txtCountry.Text & "','" & txtEMail.Text & "','"             & txtPhone.Text & "','" & txtUsername.Text & "','" & txtPassword.Text &             "')", Application("objSqlconnection"))             sqlCommand.ExecuteNonQuery()             Session("UserName") = txtUsername.Text             Session("Password") = txtPassword.Text             Session("CustomerID") = intNewCustomerID              Catch ex As Exception             lblMessage.Visible = True             lblMessage.Text = "Error in saving customer information."           End Try           Else           'The user name exists           lblMessage.Visible = True          lblMessage.Text = txtUsername.Text & " User name exists."           Exit Sub        End If     End If    'If the new customer is added successfully, go to the BookStore page        Response.Redirect("BookStore.aspx")        End Sub       Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load       txtUsername.Attributes.Add("AUTOCOMPLETE", "Off")     End Sub End Class 
end example
 

Download this Listing .

In the above listing, the cmdSubmit_Click() method executes when an end user specifies the information required to register to the application and clicks the Submit button. This method checks the validity of the information supplied by a prospective customer. If the information is valid, this method saves the customer information in the database, retrieves the Customer Id of the saved record, and defines the CustomerId and UserName session variables . This method then invokes the Book Store Web page.

Creating the Book Store Web Page

Using the Book Store Web page, an end user can search for books sorted into various categories and select books for purchase.

Listing 3-14 shows the design for the BookStore.aspx file:

Listing 3-14: GUI for the Book Store Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="BookStore.aspx.vb" Inherits="OnlineBookStore.BookStore" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Book Store</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> <script language="javascript"> function BookInfo(boolAddToCart,bookId)  { document.Form1.action="bookstore.aspx?AddBookToCart=" + boolAddToCart + "&BookId=" + bookId;  document.Form1.submit();   } function ViewCart()  { document.Form1.action="ViewCart.aspx"; document.Form1.submit();   } </script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:label id="lblMessage" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server" ForeColor="White" BackColor="Blue" Font-Names="Verdana" Font-Bold="True" Font-Size="Large" Width="328px"> Welcome </asp:label> <asp:label id="lblSelectBookCategory" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 112px" runat="server" Font-Names="Verdana" Font-Size="Smaller" Width="160px" Height="23px">Select Book Category </asp:label> <asp:label id="lblError" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Font-Names="Verdana" Font-Size="Smaller" Width="384px" Height="32px" Visible="False"> </asp:label> <asp:dropdownlist id="bookCategoryCombo" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 112px" runat="server" Font-Names="Verdana" Width="224px" Height="24px" AutoPostBack="True"> </asp:dropdownlist> <asp:table id="tblBookInformation" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 152px" runat="server" Font-Names="Verdana" Font-Size="Smaller" Width="376px" Height="64px"> </asp:table> <asp:imagebutton id="cmdLogout" style="Z-INDEX: 106; LEFT: 616px; POSITION: absolute; TOP: 56px" runat="server" Width="120px" Height="24" ImageUrl=".\Images\Logout.jpg"> </asp:imagebutton> <A href="javascript:onclick=ViewCart()"></A> <asp:HyperLink id="linkViewCart" style="Z-INDEX: 107; LEFT: 616px; POSITION: absolute; TOP: 96px" runat="server" Width="104px" Height="24px" ImageUrl=".\Images\ViewCart.jpg" NavigateUrl="ViewCart.aspx"> </asp:HyperLink> </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the Book Store Web page.

Listing 3-15 shows the code for the BookStore.aspx file:

Listing 3-15: The BookStore.aspx File
start example
 Imports System.Data.SqlClient Public Class BookStore    Inherits System.Web.UI.Page    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label    Dim sqlCommand As SqlCommand    Dim sqlda As SqlDataAdapter    Protected WithEvents lblError As System.Web.UI.WebControls.Label    Protected WithEvents bookCategoryCombo As System.Web.UI.WebControls.DropDownList    Protected WithEvents tblBookInformation As System.Web.UI.WebControls.Table    Protected WithEvents cmdLogout As System.Web.UI.WebControls.ImageButton    Dim ds As DataSet    Protected WithEvents linkViewCart As System.Web.UI.WebControls.HyperLink    Protected WithEvents lblSelectBookCategory As System.Web.UI.WebControls.Label    Dim objBookCart As ArrayList    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    MyBase.Load    'Check whether the customer login credentials are found    If Session("CustomerId") Is Nothing Then       Response.Redirect("Login.aspx")       Exit Sub    End If    If CStr(Session("CustomerId")) = "" Then       Response.Redirect("Login.aspx")       Exit Sub    End If    'Initialize the shopping cart object in the session, if it is empty.    If Session("BookCart") Is Nothing Then       objBookCart = New ArrayList       Session("BookCart") = objBookCart       linkViewCart.Visible = False       Else       objBookCart = Session("BookCart")    If Not objBookCart.Count = 0 Then       linkViewCart.Visible = True    End If    End If    If Not Page.IsPostBack = True Then       lblMessage.Text = lblMessage.Text & " " & Session("username")       lblError.Visible = False       'Retrieve books categories from database       Try          sqlCommand = New SqlCommand("select * from [Books Category]", Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)          ds = New DataSet          sqlda.Fill(ds)          Catch ex As Exception          lblError.Visible = True          lblError.Text = "Unable to retrieve Books Categories."          Exit Sub       End Try       lblError.Visible = False       If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then          Dim rowcount As Integer          For rowcount = 0 To ds.Tables(0).Rows.Count - 1          bookCategoryCombo.Items.Add(ds.Tables(0).Rows(rowcount).Item(1))          Next          Else          lblError.Visible = True          lblError.Text = "No Books Category found"    End If    bookCategoryCombo_SelectedIndexChanged(bookCategoryCombo, New EventArgs)    Else    'If there is a request made to add a book to the cart.       If Not Request("AddBookToCart") Is Nothing Then          If Request("AddBookToCart") = "TRUE" Then          Try             sqlCommand = New SqlCommand("select name,price from Books where BookId=" & Request("BookId")             & "", Application("objSqlconnection"))             sqlda = New SqlDataAdapter(sqlCommand)             ds = New DataSet             sqlda.Fill(ds)             Catch ex As Exception             lblError.Visible = True             lblError.Text = "Unable to add the book to the Cart."             Exit Sub          End Try    If Not ds.Tables(0).Rows.Count = 0 Then       'Create an object of the type BookObject to hold the values of the book to be added in the cart       Dim objBookObject As New BookObject       objBookObject.Id = Request("BookId")       objBookObject.Name = ds.Tables(0).Rows(0).Item(0) objBookObject.Price =       ds.Tables(0).Rows(0).Item(1)       'Add the objBookObject to the objBookCart       objBookCart.Add(objBookObject)       linkViewCart.Visible = True       Session("BookCart") = objBookCart    End If    End If    End If    If bookCategoryCombo.Items.Count <> 0 Then       bookCategoryCombo_SelectedIndexChanged(bookCategoryCombo, New EventArgs)    End If    End If    End Sub    Private Sub bookCategoryCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As       System.EventArgs) Handles bookCategoryCombo.SelectedIndexChanged       'Clear the table tblBookInformation       tblBookInformation.Rows.Clear()       'Retrieve books that belongs to the selected book category       Try          sqlCommand = New SqlCommand("select * from Books where CategoryId=(select CategoryId from [Books          Category] where Category='" & bookCategoryCombo.SelectedItem.Text & "')",          Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)          ds = New DataSet          sqlda.Fill(ds)          Catch ex As Exception          lblError.Visible = True          lblError.Text = "Unable to retrieve books for the selected book category."          Exit Sub       End Try       lblError.Visible = False       If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then          Dim rowcount As Integer          tblBookInformation.GridLines = GridLines.Horizontal          For rowcount = 0 To ds.Tables(0).Rows.Count - 1             'This variable is used to store the rendering HTML of the current book             tblBookInformation.Rows.Add(New System.Web.UI.WebControls.TableRow)             Dim strHTML As String = ""             strHTML = strHTML & "<TABLE border=0>"             'Get the image URL of the book             Dim strImageURL As String = ds.Tables(0).Rows(rowcount).Item(7)             'Show the book image.             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><IMG src='" & strImageURL & "' width=200             height=200></IMG></TD>"             strHTML = strHTML & "<TD valign='top'>"             'Display the book information.             StrHTML = strHTML & "<TABLE border=0>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Id: </B>" &             ds.Tables(0).Rows(rowcount).Item(0) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Name: </B>" &             ds.Tables(0).Rows(rowcount).Item(1) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Author: </B>" &             ds.Tables(0).Rows(rowcount).Item(3) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Publisher: </B>" &             ds.Tables(0).Rows(rowcount).Item(4) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Price: $ </B>" &             ds.Tables(0).Rows(rowcount).Item(5) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD><FONT size=1><B>Description: </B>" &             ds.Tables(0).Rows(rowcount).Item(6) & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "<TR>"             strHTML = strHTML & "<TD>"             'Add the hyperlink to allow addition of book to the shopping cart.             strHTML = strHTML & "<A href='#'><IMG src='./Images/AddToCart.jpg'             onclick=BookInfo('TRUE','" & ds.Tables(0).Rows(rowcount).Item(0) &             "')></IMG></A>"             strHTML = strHTML & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "</TABLE>"             strHTML = strHTML & "</TD>"             strHTML = strHTML & "</TR>"             strHTML = strHTML & "</TABLE>"             'Add the HTML to the screen.             tblBookInformation.Rows(tblBookInformation.Rows.Count - 1).Cells.Add(New             System.Web.UI.WebControls.TableCell)             tblBookInformation.Rows(tblBookInformation.Rows.Count -             1).Cells(tblBookInformation.Rows(tblBookInformation.Rows.Count - 1).Cells.Count - 1).Text =             strHTML          Next          Else          lblError.Visible = True          lblError.Text = "No Books found for the selected Book Category."       End If    End Sub    Private Sub cmdLogout_Click(ByVal sender As System.Object, ByVal e As       System.Web.UI.ImageClickEventArgs) Handles cmdLogout.Click       'Clear the session information and go to login page.       Session.Clear()       Response.Redirect("Login.aspx")    End Sub    End Class    Public Class BookObject       Public Id As Integer       Public Name As String       Public Price As Double End Class 
end example
 

Download this Listing .

The above listing defines the following methods:

  • Page_Load() : Retrieves the available categories from the Books Category table and loads the categories in the bookCategoryCombo drop-down list box.

  • bookCategoryCombo_SelectedIndexChanged() : Executes when an end user selects a category to search for the books available in that category. This method retrieves information for all the books in the selected category and displays the information in the tblBookInformation table.

The end user clicks the Add to Cart link to add a book to the shopping cart. The Page_Load() method adds information about the selected book to a BookCart Session variable. The end user clicks the View Cart link to view the books in the current shopping cart. The View Cart link invokes the View Cart Web page.

Creating the View Cart Web Page

The View Cart Web page enables an end user to view the books added to the shopping cart in the Book Store page. The View Cart Web page also displays the total purchase amount for the current shopping cart and enables an end user to place an order.

Listing 3-16 shows the design for the ViewCart.aspx file:

Listing 3-16: GUI for the View Cart Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="ViewCart.aspx.vb" Inherits="OnlineBookStore.ViewCart" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>View Cart</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <meta http-equiv="Pragma" content="no-cache"> </HEAD> <body MS_POSITIONING="GridLayout" style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana"> <form id="Form1" method="post" runat="server"> <asp:Table id="tblBooksCart" style="Z-INDEX: 100; LEFT: 8px; POSITION: absolute; TOP: 96px" runat="server" Width="100%" BorderWidth="1" BorderColor="LightGray" CellPadding="0" CellSpacing="0"> </asp:Table> <asp:ImageButton id="cmdBackToBookStore" style="Z-INDEX: 111; LEFT: 640px; POSITION: absolute; TOP: 72px" runat="server" ImageUrl="./Images/BookStore.jpg"> </asp:ImageButton> <asp:ImageButton id="cmdPlaceOrder" style="Z-INDEX: 106; LEFT: 752px; POSITION: absolute; TOP: 72px" runat="server" ImageUrl="./Images/PlaceOrder.jpg"> </asp:ImageButton> <asp:label id="lblError" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server" Width="800px" ForeColor="#C00000" Height="21px" Visible="False">lblError </asp:label> <asp:Label id="lblBooksCart" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 64px" runat="server" Font-Size="Larger" Font-Bold="True">Books Cart </asp:Label> </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the View Cart Web page.

Listing 3-17 shows the code for the ViewCart.aspx file:

Listing 3-17: The ViewCart.aspx File
start example
 Public Class ViewCart    Inherits System.Web.UI.Page    Protected WithEvents lblError As System.Web.UI.WebControls.Label    Protected WithEvents tblBooksCart As System.Web.UI.WebControls.Table    Protected WithEvents cmdBackToBookStore As System.Web.UI.WebControls.ImageButton    Protected WithEvents cmdPlaceOrder As System.Web.UI.WebControls.ImageButton    'These variables are used to calculate total books and total billing amount.    Dim totalBooks As Integer    Protected WithEvents lblBooksCart As System.Web.UI.WebControls.Label    Dim totalAmount As Double = 0.0    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    MyBase.Load       'If the session does not contains the books cart, go to book store page.       If Session("BookCart") Is Nothing Then          Response.Redirect("BookStore.aspx")       End If       'Get the book cart from the session.       Dim objBooksCart As New ArrayList       objBooksCart = Session("BookCart")       'If books cart is empty, go to BookStore page.       If objBooksCart.Count = 0 Then          Response.Redirect("BookStore.aspx")       End If       'Empty the books list.       Dim i As Integer       tblBooksCart.Rows.Clear()       'Add a new row to the products list.       tblBooksCart.Rows.Add(New System.Web.UI.WebControls.TableRow)       'Set the heading row for the books list in the books cart.       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Font.Bold = True       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Text = "BookID"       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Font.Bold = True       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Text = "Book Name"       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Font.Bold = True       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).HorizontalAlign = HorizontalAlign.Right       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Text = "Price"       'For each book in the books cart.       For i = 0 To objBooksCart.Count - 1          'Retrieve the book information.          Dim objBook As New Object          objBook = objBooksCart(i)          'Add a new row to the table that displays the books in the cart          tblBooksCart.Rows.Add(New System.Web.UI.WebControls.TableRow)          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Font.Bold = False          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).BackColor = Color.White          'Display the Book ID.          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New          System.Web.UI.WebControls.TableCell)          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -          1).Cells.Count - 1).Text = objBook.Id          'Display the Book Name.          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New          System.Web.UI.WebControls.TableCell)          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -          1).Cells.Count - 1).Text = objBook.Name          'Display the Price of the Book.          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New          System.Web.UI.WebControls.TableCell)          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -          1).Cells.Count - 1).HorizontalAlign = HorizontalAlign.Right          tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -          1).Cells.Count - 1).Text = objBook.Price          'Calculate the total billing amount.          totalAmount = totalAmount + objBook.Price       Next       totalBooks = i       'Add the bottom row to the cart to display total number of books and total billing amount       tblBooksCart.Rows.Add(New System.Web.UI.WebControls.TableRow)       'Display the total billing amount at the bottom of the books cart.       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Font.Bold = True       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Text = "Total Billing Amount"       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells.Add(New       System.Web.UI.WebControls.TableCell)       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Font.Bold = True       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).HorizontalAlign = HorizontalAlign.Right       tblBooksCart.Rows(tblBooksCart.Rows.Count - 1).Cells(tblBooksCart.Rows(tblBooksCart.Rows.Count -       1).Cells.Count - 1).Text = "$ " & totalAmount.ToString       tblBooksCart.GridLines = GridLines.Both    End Sub    Private Sub cmdBackToBookStore_Click(ByVal sender As System.Object, ByVal e As _    System.Web.UI.ImageClickEventArgs) Handles cmdBackToBookStore.Click       Response.Redirect("BookStore.aspx")    End Sub    Private Sub cmdPlaceOrder_Click(ByVal sender As System.Object, ByVal e As _    System.Web.UI.ImageClickEventArgs) Handles cmdPlaceOrder.Click       Response.Redirect("PlaceOrder.aspx?Amount=" & totalAmount & "")    End Sub End Class 
end example
 

Download this Listing .

The above listing defines the following methods:

  • Page_Load() : Retrieves information on all books from the BookCart Session variable and displays the information in the tblBooksCart table.

  • cmdPlaceOrder_Click() : Executes when an end user clicks the Place Order button to place the order for the books in the shopping cart. This method invokes the Place Order page and passes the total amount of the order as a query string variable.

Creating the Place Order Web Page

The Place Order Web page enables an end user to specify credit card information to complete the order placement process.

Listing 3-18 shows the design for the PlaceOrder.aspx file:

Listing 3-18: GUI for the Place Order Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="PlaceOrder.aspx.vb" Inherits="OnlineBookStore.PlaceOrder" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Place Order</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana" MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:label id="lblError" style="Z-INDEX: 183; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" Width="800px" Visible="False" Height="48px" ForeColor="#C00000">lblError </asp:label> <asp:imagebutton id="cmdSubmit" style="Z-INDEX: 182; LEFT: 16px; POSITION: absolute; TOP: 224px" runat="server" ImageUrl="./Images/Submit.jpg"> </asp:imagebutton> <asp:label id="lblCreditCardInfo" style="Z-INDEX: 181; LEFT: 16px; POSITION: absolute; TOP: 80px" runat="server" Font-Bold="True" Width="224px">Credit Card Information </asp:label> <asp:label id="lblMandatory3" style="Z-INDEX: 180; LEFT: 408px; POSITION: absolute; TOP: 168px" runat="server" Width="112px" ForeColor="Red">* mm/dd/yyyy </asp:label> <asp:label id="lblMandatory4" style="Z-INDEX: 179; LEFT: 408px; POSITION: absolute; TOP: 192px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:label id="lblMandatory2" style="Z-INDEX: 178; LEFT: 408px; POSITION: absolute; TOP: 144px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:label id="lblMandatory1" style="Z-INDEX: 177; LEFT: 408px; POSITION: absolute; TOP: 120px" runat="server" Width="64px" ForeColor="Red">* </asp:label> <asp:label id="lblCreditCardType" style="Z-INDEX: 169; LEFT: 16px; POSITION: absolute; TOP: 120px" runat="server">Credit Card Type </asp:label> <asp:label id="lblCreditCardNumber" style="Z-INDEX: 170; LEFT: 16px; POSITION: absolute; TOP: 144px" runat="server">Credit Card Number </asp:label> <asp:textbox id="txtCreditCardNumber" style="Z-INDEX: 171; LEFT: 176px; POSITION: absolute; TOP: 136px" runat="server" Width="225px" Height="24px" MaxLength="16"> </asp:textbox> <asp:label id="lblCreditExpiredate" style="Z-INDEX: 172; LEFT: 16px; POSITION: absolute; TOP: 168px" runat="server">Expiry Date </asp:label> <asp:textbox id="txtExpiryDate" style="Z-INDEX: 173; LEFT: 176px; POSITION: absolute; TOP: 160px" runat="server" Width="225" Height="24"> </asp:textbox> <asp:label id="lblNameOnCreditCard" style="Z-INDEX: 174; LEFT: 16px; POSITION: absolute; TOP: 192px" runat="server">Name on Card </asp:label> <asp:textbox id="txtNameOnCard" style="Z-INDEX: 175; LEFT: 176px; POSITION: absolute; TOP: 184px" runat="server" Width="225" Height="24"> </asp:textbox> <asp:dropdownlist id="cmbCreditCardType" style="Z-INDEX: 176; LEFT: 176px; POSITION: absolute; TOP: 112px" runat="server" Width="224px"> </asp:dropdownlist> </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the Place Order Web page.

Listing 3-19 shows the code for the PlaceOrder.aspx file:

Listing 3-19: The PlaceOrder.aspx File
start example
 Imports System.Data.SqlClient Public Class PlaceOrder    Inherits System.Web.UI.Page    Protected WithEvents txtCreditCardNumber As System.Web.UI.WebControls.TextBox    Protected WithEvents txtNameOnCard As System.Web.UI.WebControls.TextBox    Protected WithEvents txtExpiryDate As System.Web.UI.WebControls.TextBox    Protected WithEvents cmbCreditCardType As System.Web.UI.WebControls.DropDownList    Protected WithEvents lblError As System.Web.UI.WebControls.Label    Protected WithEvents cmdSubmit As System.Web.UI.WebControls.ImageButton    Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm    Dim sqlCommand As SqlCommand    Dim sqlda As SqlDataAdapter    Protected WithEvents lblCreditCardInfo As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory3 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory4 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory2 As System.Web.UI.WebControls.Label    Protected WithEvents lblMandatory1 As System.Web.UI.WebControls.Label    Protected WithEvents lblCreditCardType As System.Web.UI.WebControls.Label    Protected WithEvents lblCreditCardNumber As System.Web.UI.WebControls.Label    Protected WithEvents lblCreditExpiredate As System.Web.UI.WebControls.Label    Protected WithEvents lblNameOnCreditCard As System.Web.UI.WebControls.Label    Dim ds As DataSet       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       txtCreditCardNumber.Attributes.Add("AUTOCOMPLETE", "Off")       txtNameOnCard.Attributes.Add("AUTOCOMPLETE", "Off")       txtExpiryDate.Attributes.Add("AUTOCOMPLETE", "Off")       If Session("UserName") = Nothing Then          Response.Redirect("Login.aspx")          Exit Sub       End If       If Session("Password") = Nothing Then          Response.Redirect("Login.aspx")          Exit Sub       End If       'If there is no book in the books cart stored in the session, go to books store page.          If Session("BookCart") Is Nothing Then          Response.Redirect("BookStore.aspx")          Exit Sub       End If       'Retrieve the available credit card types.        Try          sqlCommand = New SqlCommand("select * from CreditCardType", Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)          ds = New DataSet          sqlda.Fill(ds)          Catch ex As Exception          'Redirect to ViewCart.aspx          Response.Redirect("ViewCart.aspx")       End Try       If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then          'Add the credit card types.          Dim rowcount As Integer          For rowcount = 0 To ds.Tables(0).Rows.Count  1             cmbCreditCardType.Items.Add(ds.Tables(0).Rows(rowcount).Item(0))          Next          Else          lblError.Text = "No credit card type available. Your order cannot be saved."          lblError.Visible = True          Exit Sub       End If    End Sub    Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As       System.Web.UI.ImageClickEventArgs) Handles cmdSubmit.Click       'If there is no book in the books cart stored in the session, go to books store page.          If Session("BookCart") Is Nothing Then          Response.Redirect("BookStore.aspx")          Exit Sub       End If       'Check the validity of credit card values.       If cmbCreditCardType.SelectedItem.Text = "" Then          lblError.Text = "No credit card type available."          lblError.Visible = True          Exit Sub       End If       If txtCreditCardNumber.Text.Trim().Length = 0 Then          lblError.Text = "Please provide the credit card number."          lblError.Visible = True          Exit Sub       End If       If txtCreditCardNumber.Text.Trim().Length < 16 Then          lblError.Text = "Invalid credit card number."          lblError.Visible = True          Exit Sub       End If       If txtExpiryDate.Text.Trim().Length = 0 Then          lblError.Text = "Please provide the credit card expiry date."          lblError.Visible = True          Exit Sub       End If       Dim expiryDate As System.DateTime       If expiryDate.Today > CType(txtExpiryDate.Text, System.DateTime) Then          lblError.Text = "You credit card has expired."          lblError.Visible = True          Exit Sub       End If       If txtNameOnCard.Text.Trim().Length = 0 Then          lblError.Text = "Please provide the name on credit card."          lblError.Visible = True          Exit Sub       End If       lblError.Visible = False       'Retrieve the shopping cart from the session and store it in the order information object.        Dim objBookCart As ArrayList       objBookCart = Session("BookCart")       Dim objBook As New Object       'This variable contains the ID of the new order.       Dim intNewOrderID As Integer       Try          sqlCommand = New SqlCommand("select count(OrderId) from orders",          Application("objSqlConnection"))          sqlda = New SqlDataAdapter(sqlCommand)          ds = New DataSet          sqlda.Fill(ds)          Catch ex As Exception          lblError.Visible = True          lblError.Text = "Unable to save your order"          Exit Sub       End Try       If Not ds.Tables(0).Rows.Count = 0 Then          intNewOrderID = CInt(ds.Tables(0).Rows(0).Item(0)) + 1          Else          intNewOrderID = 1       End If       Dim sqlConnection As SqlConnection = Application("objSQlconnection")       Dim objSQLTransaction As SqlTransaction = sqlConnection.BeginTransaction       Dim dt As System.DateTime       Try          sqlCommand = New SqlCommand("insert into          Orders(OrderId,OrderDate,CustomerId,Amount,CreditCardNumber,CreditCardType,Status) values("          & intNewOrderID & ",'" & dt.Today & "'," & Session("CustomerId")           & ", " & Request("Amount") & ",'" & txtCreditCardNumber.Text & "','"           & cmbCreditCardType.SelectedItem.Text & "','P')",           Application("objSQLConnection"))          sqlCommand.Transaction = objSQLTransaction          sqlCommand.ExecuteNonQuery()          Dim i As Integer          For i = 0 To objBookCart.Count - 1          objBook = objBookCart(i)          sqlCommand = New SqlCommand("insert into OrderDetails values(" & intNewOrderID & ","          & objBook.id & ",1)", Application("objSQLConnection"))          sqlCommand.Transaction = objSQLTransaction          sqlCommand.ExecuteNonQuery()          Next          objSQLTransaction.Commit()          Catch ex As Exception          objSQLTransaction.Rollback()          lblError.Visible = True          lblError.Text = "Unable to save your order"       Exit Sub    End Try    'If the new order is added successfully, display the order id        Session.Remove("BookCart")       Session("Message") = "Your order is saved. Your Order Id is " & intNewOrderID & "."       Response.Redirect("ShowMessage.aspx")       End Sub End Class 
end example
 

Download this Listing .

The above listing defines the following methods:

  • Page_Load() : Retrieves the credit card types available from the CreditCardType table and loads the credit card types in the cmbCreditCardType drop-down list box.

  • cmdSubmit_Click() : Executes when an end user specifies the credit card information and clicks the Submit button to place the order. This method saves the order information in the database and retrieves the Order Id for that order. It then removes the BookCard variable from the Session collection to destroy the information about the current shopping cart. It creates a session variable, Message, that contains the confirmation message and the Order Id of the order placed. This method then invokes the Order Confirmation Web page.

Creating the Order Confirmation Web Page

The Order Confirmation Web page displays the confirmation message along with the Order Id of the order that an end user places in the Place Order window. The Order Confirmation Web page also provides options to the end user to log out or to navigate to the Book Store Web page.

Listing 3-20 shows the design for the ShowMessage.aspx file:

Listing 3-20: GUI for the Order Confirmation Web Page
start example
 <%@ Page Language="vb" AutoEventWireup="false"  Codebehind="ShowMessage.aspx.vb" Inherits="OnlineBookStore.ShowMessage" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Order Confirmation</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout" style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana"> <form id="Form1" method="post" runat="server"> <asp:TextBox id="txtMessage" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="576px" Height="64px" TextMode="MultiLine" ReadOnly="True" ForeColor="White" BackColor="Blue" Font-Names="Verdana" Font-Size="Medium"> </asp:TextBox> <asp:ImageButton id="cmdBookStore" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 80px" runat="server" ImageUrl=".\Images\BookStore.jpg" Height="24px" Width="120px"> </asp:ImageButton> <asp:ImageButton id="cmdLogout" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 112px" runat="server" ImageUrl=".\Images\Logout.jpg" Height="24px" Width="120px"> </asp:ImageButton> </form> </body> </HTML> 
end example
 

Download this Listing .

In the above listing, the HTML code defines the elements for the Order Confirmation Web page.

Listing 3-21 shows the code for the ShowMessage.aspx file:

Listing 3-21: The ShowMessage.aspx File
start example
 Public Class ShowMessage    Inherits System.Web.UI.Page    Protected WithEvents cmdBookStore As System.Web.UI.WebControls.ImageButton    Protected WithEvents cmdLogout As System.Web.UI.WebControls.ImageButton    Protected WithEvents txtMessage As System.Web.UI.WebControls.TextBox    Dim Message As String    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    'If the session contains a Message variable, display the message.       If Not Session("Message") Is Nothing Then       txtMessage.Text = Session("Message")    End If    End Sub    Private Sub cmdLogout_Click(ByVal sender As System.Object, ByVal e As       System.Web.UI.ImageClickEventArgs) Handles cmdLogout.Click       'Clear all session variables       Session.Clear()       'Back to login page       Response.Redirect("login.aspx")    End Sub    Private Sub cmdBookStore_Click(ByVal sender As System.Object, ByVal e As       System.Web.UI.ImageClickEventArgs) Handles cmdBookStore.Click       'Back to Book Store page       Response.Redirect("Bookstore.aspx")    End Sub    End Class 
end example
 

Download this Listing .

The above listing defines the following methods:

  • Page_Load() : Displays the value of the Message session variable in the txtMessage text box.

  • cmdLogout_Click() : Executes when an end user clicks the Logout button to log out from the session. This method clears the session variables and invokes the Login Web page.

  • cmdBookStore_Click() : Executes when an end user clicks the Book Store button to navigate to the Book Store page to browse for books.

Creating the Global.asax File

The Global.asax file provides the Global class that defines the application and session-specific methods.

Listing 3-22 shows the code for the Global.asax file:

Listing 3-22: The Global.asax File
start example
 Imports System.Web    Imports System.Web.SessionState    Imports System.IO    Imports System.Data.SqlClient    Public Class Global       Inherits System.Web.HttpApplication       Dim sqlConnection As New SqlConnection       Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)          'Establish a connection with database BookStore          Try             'Read the connectionstring property from a file Settings.txt             Dim sr As New StreamReader(Server.MapPath(".") & "/Settings.txt")             Dim strSQLServer As String             sqlConnection.ConnectionString = sr.ReadLine             sr.Close()             Catch ex As Exception             'If Settings.txt not found or not accessible                sqlConnection.ConnectionString = "Server=localhost;uid=sa;pwd=sa;initial catalog=BookStore"             End Try             sqlConnection.Open()             'Set the sqlConnection object as an application variable             Application("objSqlConnection") = sqlConnection       End Sub End Class 
end example
 

Download this Listing .

In the above listing, the Application_Start() method executes when the Online Book Store Web application starts. This method establishes a connection with the BookStore database through the Sqlconnection object and creates an application variable objSqlConnection that contains a reference to the Sqlconnection object.




NET InstantCode. UML with Visio and Visual Studio .NET
NET InstantCode. UML with Visio and Visual Studio .NET
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 49

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net