I'm trying to make a checkout and this is the module I've made. When I try to add anything to the cart, it gives me a "NullReferenceException was Unhandled" error. This error occurs on the row:
For Each objdr In ObjDT.Rows 'For each row in the Data Table
Below is the coding for my module.
**Note: DGCart is the name of my DataGridView
Module Data
Public ObjDT As System.Data.DataTable
Public objdr As System.Data.DataRow
Public product As String
Public costcart As Decimal
Public Sub CreateCart()
objdt = New system.data.datatable("Cart")
ObjDT.Columns.Add("ID", GetType(Integer)) 'Creates ID field
ObjDT.Columns("ID").AutoIncrement = True 'Makes ID field an autonumber
ObjDT.Columns("ID").AutoIncrementSeed = 1 'Autonumbering starts at 1
ObjDT.Columns.Add("Product", GetType(String)) 'Second column is called Product. This will list the product e.g. hat, socks
ObjDT.Columns.Add("Quantity", GetType(Integer)) 'Third column is the quantity being purchased - as a number
ObjDT.Columns.Add("Cost", GetType(Decimal)) 'Makes fourth column that is cost
ShoppingCart.DGCart.DataSource = ObjDT
End Sub
Public Sub AddtoCart()
Dim blnmatch As Boolean = False
For Each objdr In ObjDT.Rows 'For each row in the Data Table
If objdr("Product") = product Then
objdr("Quantity") += ShoppingCart.ItemQuantity.Text
blnmatch = True
Exit For
End If
Next
If Not blnmatch Then
objdr = ObjDT.NewRow
objdr("Quantity") = ShoppingCart.ItemQuantity.Text
objdr("Product") = product
objdr("Cost") = costcart
ObjDT.Rows.Add(objdr)
End If
blnmatch = False
End Sub
Function GetTotalCost() As Decimal
Dim intcounter As Integer
Dim RunningTotal As Decimal
intcounter = 0
For intcounter = 0 To ObjDT.Rows.Count - 1
objdr = ObjDT.Rows(intcounter)
RunningTotal += (objdr("Cost") * objdr("Quantity"))
Next
Return RunningTotal
End Function
End Module
This post has been edited by Martyr2: 08 August 2012 - 10:30 PM
Reason for edit:: Notice closing code tags contain a forward slash "/" :)

New Topic/Question
Reply



MultiQuote




|