vendredi 31 juillet 2015

how do i get purchased items to a gridview in a shopping cart?

Hey I recently created a shopping cart but I'm having problems sorting the purchased items into a gridview. this is my cart class:

Public Class Cart
    Private dt As DataTable = New DataTable()

    Public Sub New()
        dt.Columns.Add(New DataColumn("Product ID"))
        dt.Columns.Add(New DataColumn("Quantity"))
        dt.PrimaryKey = New DataColumn() {dt.Columns("Product ID")}
    End Sub

    Public Sub AddToCart(ByVal prd_id As Integer, ByVal quantity As Integer)
        Dim dr As DataRow = dt.NewRow()
        dr("Product ID") = prd_id
        dr("Quantity") = quantity
        dt.Rows.Add(dr)
    End Sub

    Public Sub RemoveFromCart(ByVal prd_id As Integer)
        Dim dr As DataRow = dt.Rows.Find(prd_id)
        dt.Rows.Remove(dr)
    End Sub

    Public Function GetCart() As DataTable
        Return dt
    End Function
End Class

this is the button function:

If Session("Customer_ID") <> Nothing Then
Dim userCart As Cart = CType(Session("shoppingCart"), Cart)
Dim qty As Integer = txtqty.text
Dim pid As Integer = lblid.text
userCart.AddToCart(pID, qty)
Else 
Response.Redirect("User_Login.aspx")
End If

when I try to run the code I get an error saying that ("Object reference not set to an instance of an object.") please help I've completely ran out of ideas. how can i fix this?

Aucun commentaire:

Enregistrer un commentaire