Saturday, April 02, 2011

Code For Shopping Cart


'----------------------------------------------------------------
' Sub UpdateButton_click:
'   Update the quantity of item(s) in the shopping cart and then display the contents 
'     of the cart
' Parameters:
'   [in] sender: not used
'   [in] e: not used
'----------------------------------------------------------------
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim cartHasItems As Boolean  'Set if the cart contains items
    Dim i As Integer      'loop index
    Dim quantityString as String 'used for conversion checks
    Dim quantity as Integer 'used to set the items updated quanity
    Dim item As DataGridItem    'used to access the individual grid items
    Dim quantityTextBox As TextBox  'used to access the individual TextBoxes
    Dim row As DataRow 'used to access the individual DataRows

    With ShoppingCart(False)
        If Not .IsEmpty Then
            
            If Not Page.IsValid Then 
                Return
            End If
        
            '
            ' Validators have run so we can assume valid input
            '
            
            '
            ' reset the quanities for all the items in cart
            '
            For i = 0 To CartItemsDataGrid.Items.Count - 1
                item = CartItemsDataGrid.Items(i)
                quantityTextBox = CType(Item.FindControl("QuantityTextBox"), TextBox)
                quantityString = quantityTextBox.Text
                quantity = CInt(quantityString)
                row = .OrderItems.Rows(i)
                'update the quantity
                row(1) = quantity
            Next i
            
            .UpdateItems()
            
            cartHasItems = Not .IsEmpty
            If cartHasItems Then
                'Bind the DataGrid to the items
                CartItemsDataGrid.DataSource = CType(.OrderItems.DefaultView, System.Collections.ICollection)
                CartItemsDataGrid.DataBind
            End If                    
        End If
    End With
    
    '
    ' Set visibility of displayed items to correspond to
    '   whether or not we have items in the cart.
    '
    ShoppingCartPanel.Visible = cartHasItems
    CartItemsDataGrid.Visible = cartHasItems
    CheckOutHyperLink.Visible= cartHasItems
    EmptyCartLabel.Visible = Not cartHasItems
End Sub    

'----------------------------------------------------------------
' Sub UpdateItems:
'   Updates the Items in the shopping cart
'----------------------------------------------------------------
Public Sub UpdateItems()
    Dim row As DataRow  'used to access the individual rows in OrderItems
    Dim quantity As Integer
    Dim itemCount As Integer = OrderItems.Rows.Count
    Dim i As Integer = 0
    
    
    Do While ( i < itemCount)
        With OrderItems.Rows(i)
            quantity = CInt(.Item(OrderData.QUANTITY_FIELD))
            if quantity < 1 Then 
                .Delete
                itemCount = itemCount - 1 
            Else
                .Item(OrderData.EXTENDED_FIELD) = New Decimal(quantity  * CType(.Item(OrderData.PRICE_FIELD), Decimal))
                i = i + 1
            End If
        End With
    Loop 
End Sub

No comments:

Post a Comment

Thanks for showing your interest
I will shortly get back to you