Hi,

I have following code to make a shopping cart and when I run it I get an error saying "Object reference not set to an instance of an object" and the error is in this line,

VB Code:
  1. "Line 51:         For Each objDR In objDT.Rows"
Its the line in SUB AddToCart...

VB Code:
  1. Private objDT As System.Data.DataTable
  2.     Private objDR As System.Data.DataRow
  3.  
  4.     Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
  5.         If Not IsPostBack Then
  6.             makeCart()
  7.         End If
  8.     End Sub
  9.  
  10.     Function makeCart()
  11.         objDT = New System.Data.DataTable("Cart")
  12.         objDT.Columns.Add("ID", GetType(Integer))
  13.         objDT.Columns("ID").AutoIncrement = True
  14.         objDT.Columns("ID").AutoIncrementSeed = 1
  15.  
  16.         objDT.Columns.Add("Quantity", GetType(Integer))
  17.         objDT.Columns.Add("Product", GetType(String))
  18.         objDT.Columns.Add("Cost", GetType(Decimal))
  19.  
  20.         Session("Cart") = objDT
  21.     End Function
  22.  
  23.     Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
  24.         objDT = Session("Cart")
  25.         Dim Product = ddlProducts.SelectedItem.Text
  26.         Dim blnMatch As Boolean = False
  27.  
  28.         For Each objDR In objDT.Rows
  29.             If objDR("Product") = Product Then
  30.                 objDR("Quantity") += txtQuantity.Text
  31.                 blnMatch = True
  32.                 Exit For
  33.             End If
  34.         Next
  35.  
  36.         If Not blnMatch Then
  37.             objDR = objDT.NewRow
  38.             objDR("Quantity") = txtQuantity.Text
  39.             objDR("Product") = ddlProducts.SelectedItem.Text
  40.             objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
  41.             objDT.Rows.Add(objDR)
  42.         End If
  43.         Session("Cart") = objDT
  44.  
  45.         dg.DataSource = objDT
  46.         dg.DataBind()
  47.  
  48.         lblTotal.Text = "$" & GetItemTotal()
  49.     End Sub
  50.  
  51.     Function GetItemTotal() As Decimal
  52.         Dim intCounter As Integer
  53.         Dim decRunningTotal As Decimal
  54.  
  55.         For intCounter = 0 To objDT.Rows.Count - 1
  56.             objDR = objDT.Rows(intCounter)
  57.             decRunningTotal += (objDR("Cost") * objDR("Quantity"))
  58.         Next
  59.  
  60.         Return decRunningTotal
  61.     End Function
  62.  
  63.     Sub Delete_Item(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
  64.         objDT = Session("Cart")
  65.         objDT.Rows(e.Item.ItemIndex).Delete()
  66.         Session("Cart") = objDT
  67.  
  68.         dg.DataSource = objDT
  69.         dg.DataBind()
  70.  
  71.         lblTotal.Text = "$" & GetItemTotal()
  72.     End Sub

Can anyone tell me how to create an object of this type...???

Cheers...