Object reference not set to an instance of an object.
-
Hi All I am getting a excpetion when i run my code, it was taken from the below url http://www.sitepoint.com/article/net-shopping-cart-datatables[^] I have run the code when there is no code behind page as per the authors page and it runs fine but when i try and recreate the code with a code behind I run into the obj.reference exception can someone please tell me how to correct this error. Code Below Thanks
Protected Sub AddToCart(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim objDT As System.Data.DataTable Dim objDR As System.Data.DataRow objDT = Session("Cart") Dim Product = ddlProducts.SelectedItem.Text Dim blnMatch As Boolean = False For Each objDR In objDT.Rows <-------this is were the exception occurs!!!!!! If objDR("Product") = Product Then objDR("Quantity") += txtQuantity.Text blnMatch = True Exit For End If Next If Not blnMatch Then objDR = objDT.NewRow objDR("Quantity") = txtQuantity.Text objDR("Product") = ddlProducts.SelectedItem.Text objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value) objDT.Rows.Add(objDR) End If Session("Cart") = objDT dg.DataSource = objDT dg.DataBind() lblTotal.Text = "$" & GetItemTotal() End Sub
-
Hi All I am getting a excpetion when i run my code, it was taken from the below url http://www.sitepoint.com/article/net-shopping-cart-datatables[^] I have run the code when there is no code behind page as per the authors page and it runs fine but when i try and recreate the code with a code behind I run into the obj.reference exception can someone please tell me how to correct this error. Code Below Thanks
Protected Sub AddToCart(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim objDT As System.Data.DataTable Dim objDR As System.Data.DataRow objDT = Session("Cart") Dim Product = ddlProducts.SelectedItem.Text Dim blnMatch As Boolean = False For Each objDR In objDT.Rows <-------this is were the exception occurs!!!!!! If objDR("Product") = Product Then objDR("Quantity") += txtQuantity.Text blnMatch = True Exit For End If Next If Not blnMatch Then objDR = objDT.NewRow objDR("Quantity") = txtQuantity.Text objDR("Product") = ddlProducts.SelectedItem.Text objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value) objDT.Rows.Add(objDR) End If Session("Cart") = objDT dg.DataSource = objDT dg.DataBind() lblTotal.Text = "$" & GetItemTotal() End Sub
thedom2 wrote:
objDT = Session("Cart")
You should check the session is null before assigning to an object. Session will be null, so object won't get reference and throws the specified exception.
-
thedom2 wrote:
objDT = Session("Cart")
You should check the session is null before assigning to an object. Session will be null, so object won't get reference and throws the specified exception.
-
thedom2 wrote:
why did it work when there was no code behind form ?
Means ? It won't work until your session contains a valid
DataTable
object with rows.
-
thedom2 wrote:
why did it work when there was no code behind form ?
Means ? It won't work until your session contains a valid
DataTable
object with rows.
-
but the code remains the same from the example on the website the only difference is i have incorporated a code behind file ? so i dont quite understand why it works when there is no code-behind ????
thedom2 wrote:
so i dont quite understand why it works when there is no code-behind ????
See this is not the matter you have code behind or not. I reiterate, it's the problem of session's NULL value. So check session is NULL before proceeding. By the way, where you are assigning value to the session ? I have seen you are assigning value to session after you refer it. To refer a value from session, it needs to be created before
-
thedom2 wrote:
so i dont quite understand why it works when there is no code-behind ????
See this is not the matter you have code behind or not. I reiterate, it's the problem of session's NULL value. So check session is NULL before proceeding. By the way, where you are assigning value to the session ? I have seen you are assigning value to session after you refer it. To refer a value from session, it needs to be created before
-
Well, you can check NULL in session like
if ( Session["cart"] != NULL )
//Do DataTable casting here.
else
//Create session and do casting hereIt's a c# code, you can convert into VB.NET, Let me know if it helps
-
Well, you can check NULL in session like
if ( Session["cart"] != NULL )
//Do DataTable casting here.
else
//Create session and do casting hereIt's a c# code, you can convert into VB.NET, Let me know if it helps
-