All pastes #973137 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-05 22:55 UTC
rendered paste body
Imports System.Data
Partial Class DisplayItems
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            FormMessage.Text = ""
            
            '
            ' refresh the current Cart after browsing the web site
            '
            Dim Cart As DataSet = RetrieveCart()
            If Cart.Tables(0).Rows.Count > 0 Then
                '
                ' rebuild the display cart complete using cookie
                '
                Dim Displaycart As DataSet = rebuilddisplaycart(Cart)
                InvoiceDetailsView.DataSource = Cart.Tables(0)
                InvoiceDetailsView.DataBind()
            End If
            If Not (HttpContext.Current.Request.Cookies("MarkFields") Is Nothing) Then
                txt_MemberId.Text = Request.Cookies("MarkFields")("MemberID")
                txt_QuantityId.Text = Request.Cookies("MarkFields")("ProductId")

            End If
        End If
    End Sub

    Private Function RetrieveCart() As DataSet
        Dim Cart As New DataSet
        '
        ' determine if a current cookie exists containing the current Cart Contents
        '
        If Not (HttpContext.Current.Request.Cookies("ShoppingCart") Is Nothing) Then

            Dim CartXML As String = HttpContext.Current.Request.Cookies("ShoppingCart").Value.Trim
            Cart.ReadXml(New System.Xml.XmlTextReader(New System.IO.StringReader(CartXML)))
        Else

            Dim CartTable As New DataTable

            'CartTable.Columns.Add("I", GetType(String)) 'remove
            CartTable.Columns.Add("P", GetType(Integer))
            CartTable.Columns.Add("Q", GetType(Integer))
            CartTable.Columns.Add("Pr", GetType(Decimal))

            CartTable.TableName = "CartTable"
            Cart.DataSetName = "Cart"

            Cart.Tables.Add(CartTable)

        End If
       
        Return Cart
    End Function
    Private Sub SaveCart(ByVal Cart As DataSet)
        Dim CartXML As New System.Text.StringBuilder
        Dim CartStream As New System.IO.StringWriter(CartXML)
        Cart.WriteXml(CartStream)

        '
        ' to save space on the 4K cookie remove unnecessary characters
        '
        Dim XMLCompressedString As New System.Text.StringBuilder

        For n As Integer = 0 To CartXML.Length - 1
            Dim c As Char = CartXML.Chars(n)
            If Not (Char.IsControl(c) Or Char.IsWhiteSpace(c)) Then
                XMLCompressedString.Append(c)
            End If
        Next
        '
        ' place the compressed xml string into the session cookie
        '
        Dim xmlcookie As New HttpCookie("ShoppingCart")
        xmlcookie.Value = XMLCompressedString.ToString
        HttpContext.Current.Response.Cookies.Add(xmlcookie)
        '
        ' you can save individual fields to cookies
        '
        Response.Cookies("MarkFields")("MemberID") = txt_MemberId.Text.Trim.ToString
        Response.Cookies("MarkFields")("ProductID") = txt_QuantityId.Text.Trim.ToString
    End Sub

    Protected Sub InvoiceDetailsView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles InvoiceDetailsView.RowEditing
        ''
        '' retrieve the needed table definition
        ''
        'Dim Cart As DataSet = RetrieveCart()
        ''
        '' process the current gridview via the Dump
        ''
        'Cart = DumpCart()
        ''
        '' re bind the update dataset to the gridview
        ''
        'InvoiceDetailsView.DataSource = Cart.Tables(0)
        'InvoiceDetailsView.DataBind()
        'SaveCart(Cart)


        'Hide the footer during edits...
        InvoiceDetailsView.EditIndex = e.NewEditIndex
        ' Bind DataGrid to datasource
        Dim RaceInfoViewDS As DataSet = DisplayCartDefinition()
        InvoiceDetailsView.DataSource = RaceInfoViewDS.Tables(0)
        InvoiceDetailsView.DataBind()

    End Sub
    Protected Sub InvoiceDetailsView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles InvoiceDetailsView.RowCancelingEdit
        '
        ' this method is called when the Cancel button on the editted row is pressed
        '
        ' reset the EditIndex to its off position, this will cause the display template to be use
        '
        InvoiceDetailsView.EditIndex = -1
        '
        ' Refresh the gridview using the orignal cart in the cookie
        '
        Dim Cart As DataSet = RetrieveCart()
        '
        ' rebuild the display cart complete using cookie
        '
        InvoiceDetailsView.DataSource = Cart.Tables(0)
        InvoiceDetailsView.DataBind()

    End Sub

    Protected Sub InvoiceDetailsView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles InvoiceDetailsView.RowUpdating

        Dim cart As DataSet = RetrieveCart()
        Dim eRaceInfoRow As DataRow = cart.Tables(0).Rows(InvoiceDetailsView.EditIndex)
        Dim gViewRow As GridViewRow = InvoiceDetailsView.Rows(InvoiceDetailsView.EditIndex)
        Dim aTextbox As TextBox
        Dim bTextbox As TextBox
        '
        ' attach yourself to the textbox control on the gridview
        '
        aTextbox = CType(gViewRow.FindControl("P"), TextBox)
        bTextbox = CType(gViewRow.FindControl("Q"), TextBox)
        If Not (aTextbox Is Nothing And bTextbox Is Nothing) Then


            eRaceInfoRow("P") = aTextbox.Text.Trim
            eRaceInfoRow("Q") = bTextbox.Text.Trim

            '
            ' reset the EditIndex to its off position, this will cause the display template to be use
            '
            InvoiceDetailsView.EditIndex = -1
            '
            ' Refresh the gridview using the updated cart in the dataset
            '
            InvoiceDetailsView.DataSource = cart.Tables(0)
            InvoiceDetailsView.DataBind()
            '
            ' Save the updated dataset to the cookie
            '
            SaveCart(cart)
        Else
            FormMessage.Text = "Application error. Contact your analysis"
        End If
    End Sub

    Protected Sub bt_Fetch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_Fetch.Click
        txt_MemberId.Text = txt_MemberId.Text.Trim
        If txt_MemberId.Text.Length = 0 Then
            FormMessage.Text = "Please enter a member id to be searched."
        Else
            Dim SystemMgr As New eRaceSystem.eRace
            Dim MemberInfo As DataSet
            Try
                MemberInfo = SystemMgr.LookupMemberByMemberId(Integer.Parse(txt_MemberId.Text))
                If MemberInfo.Tables(0).Rows.Count = 0 Then
                    FormMessage.Text = "Member does not exist."
                    txt_MemberId.Text = ""
                    Name.Text = ""
                    Phone.Text = ""
                    txt_CardNo.Text = ""
                Else
                    With MemberInfo.Tables(0).Rows(0)
                        txt_MemberId.Text = .Item("MemberId")
                        Name.Text = .Item("Name")
                        Phone.Text = .Item("Phone")
                        CreditBalance.Text = .Item("CreditBalance")
                    End With
                End If
            Catch ex As Exception
                FormMessage.Text = ex.Message
            End Try
        End If
    End Sub


    Protected Sub bt_Products_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_Products.Click
        Response.Redirect("Products.aspx")

    End Sub

    Protected Sub bt_SubmitSale_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_SubmitSale.Click
        '
        'check the radiobutton list for a selected value
        '
        Dim paymenttype As Integer = 0

        If PanelChoice.SelectedValue = "Cash" And txt_CardNo.Text.Length > 0 Then
            paymenttype = 0
            FormMessage.Text = "Please remove number from Card Number box"

        ElseIf PanelChoice.SelectedValue = "Account" And txt_CardNo.Text.Length > 0 Then
            paymenttype = 1
            FormMessage.Text = "Please remove number from Card Number box"

        ElseIf PanelChoice.SelectedValue = "Debit" And txt_CardNo.Text.Length < 12 Or txt_CardNo.Text.Length > 16 Then
            paymenttype = 2
            FormMessage.Text = "Please enter a valid card number"

        ElseIf PanelChoice.SelectedValue = "Credit" And txt_CardNo.Text.Length < 12 Or txt_CardNo.Text.Length > 16 Then
            paymenttype = 3
            FormMessage.Text = "Please enter a valid card number"

        End If
        Dim SystemMgr As New eRaceSystem.eRace
        'SystemMgr.AddInvoice(Integer.Parse(txt_MemberId), txt_CardNo, paymenttype, GST, Total, DumpCart())
        FormMessage.Text = "Success. The database was updated with the provided information."
        InvoiceDetailsView.DataSource = Nothing
        InvoiceDetailsView.DataBind()
    End Sub

    Protected Sub bt_RemoveItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_RemoveItem.Click
        Dim cart As DataSet

        Dim Index As Integer

        cart = RetrieveCart()
        Index = cart.Tables(0).Rows.Count - 1

        Dim RemoveItem As New CheckBox

        While Index >= 0
            RemoveItem = CType(InvoiceDetailsView.Rows(Index).FindControl("Remove"), CheckBox)

            If RemoveItem.Checked = True Then

                cart.Tables(0).Rows(Index).Delete()
            End If
            Index = Index - 1
        End While
        If cart.Tables(0).Rows.Count <> 0 Then
            Dim displaycart As DataSet = rebuilddisplaycart(cart)

            InvoiceDetailsView.DataSource = displaycart.Tables(0)
            InvoiceDetailsView.DataBind()
            SaveCart(cart)
        Else
            'clear grid
            'destroy cookie (this routine destroy all cookie cart and individual)
            ClearCart()
        End If
    End Sub

    Protected Sub bt_Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_Add.Click
        Dim SystemMgr As New eRaceSystem.eRace
        Dim ProductInfo As DataSet
        Try
            ProductInfo = SystemMgr.LookupProductByProductId(Integer.Parse(txt_QuantityId.Text))
            If ProductInfo.Tables(0).Rows.Count = 0 Then
                FormMessage.Text = "Product does not exist."
            Else
                '
                ' appends to cookie cart (dataset)
                '
                Dim Cart As DataSet = RetrieveCart()
                Dim aRow As DataRow
                aRow = Cart.Tables(0).NewRow
                With ProductInfo.Tables(0).Rows(0)


                    aRow("P") = txt_ProductId.Text
                    aRow("Q") = txt_QuantityId.Text
                    aRow("PR") = .Item("SalesPrice")

                End With
                Cart.Tables(0).Rows.Add(aRow)

                SaveCart(Cart)
                '
                ' appends to display dataset
                '
                Dim displayCart As DataSet = DumpCart()
                Dim dRow As DataRow = displayCart.Tables(0).NewRow
                With ProductInfo.Tables(0).Rows(0)

                    dRow("I") = .Item("Description")
                    dRow("P") = txt_ProductId.Text
                    dRow("Q") = txt_QuantityId.Text
                    dRow("PR") = .Item("SalesPrice")

                End With
                displayCart.Tables(0).Rows.Add(dRow)
                InvoiceDetailsView.DataSource = displayCart.Tables(0)
                InvoiceDetailsView.DataBind()
            End If
        Catch ex As Exception
            FormMessage.Text = ex.Message
        End Try

    End Sub
    Private Function DisplayCartDefinition() As DataSet
        Dim Cart As New DataSet
       

        Dim CartTable As New DataTable

        CartTable.Columns.Add("I", GetType(String))
        CartTable.Columns.Add("P", GetType(Integer))
        CartTable.Columns.Add("Q", GetType(Integer))
        CartTable.Columns.Add("Pr", GetType(Decimal))

        CartTable.TableName = "CartTable"
        Cart.DataSetName = "Cart"

        Cart.Tables.Add(CartTable)

        
        Return Cart
    End Function
    Public Function rebuilddisplaycart(ByVal cart As DataSet) As DataSet
        '
        ' need the definition for the display dataset
        '
        Dim displaycart As DataSet = DisplayCartDefinition()
        Dim SystemMgr As New eRaceSystem.eRace
        Dim ProductInfo As DataSet
        Try
            For Each CartRow As DataRow In cart.tables(0).rows
                ProductInfo = SystemMgr.LookupProductByProductId(Integer.Parse(CartRow.Item("P")))
                If ProductInfo.Tables(0).Rows.Count = 0 Then
                    FormMessage.Text = "Product does not exist."
                Else

                    '
                    ' appends to display dataset
                    '

                    Dim dRow As DataRow = displayCart.Tables(0).NewRow
                    With ProductInfo.Tables(0).Rows(0)

                        dRow("I") = .Item("Description")
                        dRow("P") = CartRow.Item("ProductId")
                        dRow("Q") = CartRow.Item("Quantity")
                        dRow("PR") = CartRow.Item("SalesPrice")

                    End With
                    displayCart.Tables(0).Rows.Add(dRow)

                End If
            Next
        Catch ex As Exception
            FormMessage.Text = ex.Message
        End Try
        Return cart

    End Function
    Private Function DumpCart() As DataSet
        Dim displaycart As DataSet = DisplayCartDefinition()
        Dim aRow As DataRow

        For Each gRow As GridViewRow In InvoiceDetailsView.Rows
            aRow = displaycart.Tables(0).NewRow
            aRow(0) = gRow.Cells(0).Text
            aRow(1) = gRow.Cells(1).Text
            aRow(2) = gRow.Cells(2).Text
            aRow(3) = gRow.Cells(3).Text
            displaycart.Tables(0).Rows.Add(aRow)
        Next

        Return displaycart
    End Function

    Protected Sub ClearCart()
        Dim aCookie1 As New HttpCookie("ShoppingCart")
        aCookie1.Expires = DateAdd(DateInterval.Day, -30, Today)
        HttpContext.Current.Response.Cookies.Add(aCookie1)

        Dim aCookie2 As New HttpCookie("MarkFields")
        aCookie2.Expires = DateAdd(DateInterval.Day, -30, Today)
        HttpContext.Current.Response.Cookies.Add(aCookie2)
        '
        ' also remove the display
        '
        InvoiceDetailsView.DataSource = Nothing
        InvoiceDetailsView.DataBind()
        GST.Text = 0.0
        Total.Text = 0.0
    End Sub
End Class