Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Object reference not set to an instance of an object.

Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved ASP.NET
comhelptutorial
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thedom2
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • T thedom2

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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.


      My Website | Ask smart questions

      T 1 Reply Last reply
      0
      • N N a v a n e e t h

        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.


        My Website | Ask smart questions

        T Offline
        T Offline
        thedom2
        wrote on last edited by
        #3

        why did it work when there was no code behind form ?

        N 1 Reply Last reply
        0
        • T thedom2

          why did it work when there was no code behind form ?

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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.


          My Website | Ask smart questions

          T 1 Reply Last reply
          0
          • N N a v a n e e t h

            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.


            My Website | Ask smart questions

            T Offline
            T Offline
            thedom2
            wrote on last edited by
            #5

            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 ????

            N 1 Reply Last reply
            0
            • T thedom2

              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 ????

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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


              My Website | Ask smart questions

              T 1 Reply Last reply
              0
              • N N a v a n e e t h

                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


                My Website | Ask smart questions

                T Offline
                T Offline
                thedom2
                wrote on last edited by
                #7

                can you show me please if possible ? thank you

                N 1 Reply Last reply
                0
                • T thedom2

                  can you show me please if possible ? thank you

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  Well, you can check NULL in session like

                  if ( Session["cart"] != NULL )
                  //Do DataTable casting here.
                  else
                  //Create session and do casting here

                  It's a c# code, you can convert into VB.NET, Let me know if it helps


                  My Website | Ask smart questions

                  T 1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    Well, you can check NULL in session like

                    if ( Session["cart"] != NULL )
                    //Do DataTable casting here.
                    else
                    //Create session and do casting here

                    It's a c# code, you can convert into VB.NET, Let me know if it helps


                    My Website | Ask smart questions

                    T Offline
                    T Offline
                    thedom2
                    wrote on last edited by
                    #9

                    thanks for youre help :) sorry for all questions

                    N 1 Reply Last reply
                    0
                    • T thedom2

                      thanks for youre help :) sorry for all questions

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #10

                      Welcome


                      My Website | Ask smart questions

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups