Object reference not set to an instance of an object...
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:
"Line 51: For Each objDR In objDT.Rows"
Its the line in SUB AddToCart...
VB Code:
Private objDT As System.Data.DataTable
Private objDR As System.Data.DataRow
Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
End Sub
Function makeCart()
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1
objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))
Session("Cart") = objDT
End Function
Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
Originally posted by Edneeis You can't create a datatable in that way, it has required parameters in the constructor. I believe the same for the Datarow.
I am already defining DataTable as follows...
VB Code:
objDT = New System.Data.DataTable("Cart")
But Can't find proper way to create object for datarow...
Here's a quick prog I whipped up that uses most of your code. Let me know how it runs..
CODE BEHIND
Code:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents txtQty As System.Web.UI.WebControls.TextBox
Protected WithEvents txtProduct As System.Web.UI.WebControls.TextBox
Protected WithEvents txtCost As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdAdd As System.Web.UI.WebControls.Button
Private objDR As System.Data.DataRow
Private objDT As System.Data.DataTable
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
makeCart()
Else
objDT = Session("Cart")
End If
End Sub
Function showcart()
If objDT Is Nothing Then
Response.Write("objDT is Nothing")
Exit Function
End If
Dim szLine As String
Response.Write(objDT.Rows.Count.ToString & " rows<BR>")
For Each objDR In objDT.Rows
szLine = "[Id] " & objDR("id") & " "
szLine += "[Qty] " & objDR("quantity") & " "
szLine += "[Prod] " & objDR("product") & " "
szLine += "[Qty] " & objDR("cost") & "<BR>"
Response.Write(szLine)
Next
End Function
Function makeCart()
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1
objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))
Session("Cart") = objDT
End Function
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
objDR = objDT.NewRow()
objDR("Quantity") = CInt(txtQty.Text)
objDR("Product") = txtProduct.Text
objDR("Cost") = CDec(txtCost.Text)
objDT.Rows.Add(objDR)
Session("Cart") = objDT
showcart()
End Sub
End Class