Listing N
Imports System.Threading
 
 
Public Class ViewCart
    Inherits System.Web.UI.Page
    Protected WithEvents dgCart As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
    Protected WithEvents btnCreateOrder As System.Web.UI.WebControls.Button
    Protected WithEvents lblMsg As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
 
 
#Region " Web Form Designer Generated Code "
 
 
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
 
 
    End Sub
 
 
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub
 
 
#End Region
 
 
    Private TotalPrice As Double
 
 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
 
 
        ' Load the list of ordered books
        If Not Page.IsPostBack Then
            Dim BookIds As ArrayList = CType(Session.Item(ClientSession.CLIENT_SESSION_REF), ClientSession).BookIds
 
 
            ' Ensure there are books in the session to view
            If BookIds.Count > 0 Then
                Dim ProxyObj As New BookProxy.BookWS()
                ProxyObj.BookSecurityContextValue = WSUtil.GetBookSecurityContext()
                Dim BookDs As DataSet = ProxyObj.GetBooksByIds(BookIds.ToArray)
 
 
                If Not BookDs Is Nothing Then
                    Me.dgCart.DataSource = BookDs
                    Me.DataBind()
                End If
            End If
 
 
        End If
    End Sub
 
 
    '#########################################################################################################################
    ' We need to display the total in the prerender method because the databound methods are fired after the page_load method
    '#########################################################################################################################
    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        ' Display the total price of the order
        Me.lblTotal.Text = "Total: $" + TotalPrice.ToString()
 
 
        ' If there are no items in the cart, disable the create order button
        If Me.TotalPrice = 0 Then
            Me.btnCreateOrder.Enabled = False
        End If
    End Sub
 
 
 
 
 
 
    '#########################################################################################################################
    ' When the datagrid renders each row, grab the price of the book and add it to the total
    '#########################################################################################################################
    Private Sub dgCart_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCart.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then
            TotalPrice += Convert.ToDouble(e.Item.Cells.Item(3).Text)
        End If
    End Sub
 
 
    '#########################################################################################################################
    ' Remove the selected row's book id from the session object array list
    '#########################################################################################################################
    Public Sub Remove_Item(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        CType(Session.Item(ClientSession.CLIENT_SESSION_REF), ClientSession).BookIds.Remove(e.Item.Cells(0).Text)
        Me.GetCartItems()
    End Sub
 
 
    '#########################################################################################################################
    ' Populate the datagrid with the books in the users shopping cart
    '#########################################################################################################################
    Private Sub GetCartItems()
        Dim BookIds As ArrayList = CType(Session.Item(ClientSession.CLIENT_SESSION_REF), ClientSession).BookIds
 
 
        If BookIds.Count > 0 Then
            Dim ProxyObj As New BookProxy.BookWS()
            ProxyObj.BookSecurityContextValue = WSUtil.GetBookSecurityContext()
            Dim BookDs As DataSet = ProxyObj.GetBooksByIds(BookIds.ToArray)
 
 
            If Not BookDs Is Nothing Then
                Me.dgCart.DataSource = BookDs
                Me.DataBind()
            End If
        End If
    End Sub
 
 
    '#########################################################################################################################
    ' The customer clicks this button to create their order. We create the order in this system first, generating the Pk
    ' for the order, and then invoke John's WS to create the order in his system. If the webservice fails, we delete
    ' the order from our system. John's web service is invoked asynchronously and the user is notified via email regarding
    ' the status of the order
    '#########################################################################################################################
    Private Sub btnCreateOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateOrder.Click
        Dim OrderObj As New OrderServices()
        Dim SessionObj As ClientSession = Session.Item(ClientSession.CLIENT_SESSION_REF)
 
 
        Dim OrderId As Int32 = OrderObj.CreateOrder(SessionObj.CustomerId)
 
 
        Try
            Dim ProxyObj As New OrderProxy.OrderWS()
            ProxyObj.OrderSecurityContextValue = WSUtil.GetOrderSecurityContext
            ProxyObj.BeginCreateOrder(OrderId, SessionObj.BookIds.ToArray(), New AsyncCallback(AddressOf ReceiveResponse), ProxyObj)
            Me.lblMsg.Text = "Thank you for your order"
 
 
        Catch ex As System.Web.Services.Protocols.SoapException
            ' If the web service fails for any reason, we must remove the order from the Db
            OrderObj.DeleteOrder(OrderId)
            Me.lblMsg.Text = "There was an error processing your order"
        Finally
            'Blank the datagrid of the order that was placed
            Me.dgCart.DataSource = Nothing
 
 
            ' Clean up the order information contained in the session
            SessionObj.BookIds = New ArrayList()
            SessionObj.NumItems = 0
 
 
            Me.DataBind()
        End Try
 
 
    End Sub
 
 
 
 
    '#########################################################################################################################
    ' This method is executed once John's web service completes processing. If it reaches this point, everything has
    ' executed successfully so we notify the customer via email
    '#########################################################################################################################
    Private Sub ReceiveResponse(ByVal AsyncResult As IAsyncResult)
        Dim ProxyObj As OrderProxy.OrderWS = CType(AsyncResult.AsyncState, OrderProxy.OrderWS)
 
 
        Dim Success As Boolean = ProxyObj.EndCreateOrder(AsyncResult)
        Dim SessionObj As ClientSession = CType(Session.Item(ClientSession.CLIENT_SESSION_REF), ClientSession)
 
 
        'Email the customer of the new pending order
        ' Send an email notification to the email address entered for the new user
        Dim EmailObj As New EmailUtil(Nothing, SessionObj.Email, SessionObj.FirstName + " " + SessionObj.LastName)
        Dim asyncOpIsDone As New AutoResetEvent(False)
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf EmailObj.SendPendingOrderConfirmation), asyncOpIsDone)
 
 
    End Sub
End Class