Tech Off Thread

23 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

DataTable to Session: Object Reference not set to an instance of an object

Back to Forum: Tech Off
  • User profile image
    MasterPi

    Hello C9ers!

    I'm having a problem with storing a DataTable in a Session.

    Basically, I'm getting a "Object Reference not set to an Instance of an Object" error.

    Here's the code:


    //Create a DataTable to hold Cart

    dtCart = new DataTable("Cart");

    dtCart.Columns.Add("ID", typeof(int));

    dtCart.Columns["ID"].AutoIncrement = true;

    dtCart.Columns["ID"].AutoIncrementSeed = 1;

    dtCart.Columns.Add("Product", typeof(int));

    dtCart.Columns.Add("Quantity", typeof(int));

    dtCart.Columns.Add("Cost", typeof(decimal));

    //Finally create a session object to hold this cart

    HttpContext.Current.Session["Cart"] = dtCart;


    The last line "HttpContext.Current.Session["Cart"] = dtCart;" generates the error.

    What could be the cause of this error?

    Thanks!

    EDIT: Forgot to mention that this is in a class library.

  • User profile image
    JohnAskew

    My guess is that the Session["Cart"] is undefined when the line is executed. This is from a Windows Forms guy, so take it or leave it.
    Wink

  • User profile image
    MasterPi

    JohnAskew wrote:
    My guess is that the Session["Cart"] is undefined when the line is executed. This is from a Windows Forms guy, so take it or leave it.
    Wink


    But I thought the Session is created when you store stuff in it...

  • User profile image
    JohnAskew

    I'm a Windows Forms guy, so I can't help.  :O

    Use the debugger to see exactly what in that line isn't initialized...

  • User profile image
    W3bbo

    mVPstar wrote:
    JohnAskew wrote:My guess is that the Session["Cart"] is undefined when the line is executed. This is from a Windows Forms guy, so take it or leave it.
    Wink


    But I thought the Session is created when you store stuff in it...


    Session is created during Application.Session_Start().

    Session objects in the name/value collection are created when set.

  • User profile image
    MasterPi

    W3bbo wrote:

    Session is created during Application.Session_Start().

    Session objects in the name/value collection are created when set.


    Yeah, I meant the objects.

  • User profile image
    MasterPi

    Does anyone know how to fix this?

  • User profile image
    JohnAskew

    mVPstar wrote:

    Does anyone know how to fix this?



    Something on the lefthand side of that assignment is null.

    What else do you need to know to fix that?

  • User profile image
    Shark_M

    you have to have a new instance of

    HttpContext.


    then assign to it


  • User profile image
    MasterPi

    JohnAskew wrote:
    mVPstar wrote:

    Does anyone know how to fix this?



    Something on the lefthand side of that assignment is null.

    What else do you need to know to fix that?



    Well if it is null, I would need to know exactly how to create the object, and I don't know how to create it.

  • User profile image
    MasterPi

    Shark_M wrote:
    you have to have a new instance of
    HttpContext.


    then assign to it




    Okay, I will try that tomorrow.

  • User profile image
    MasterPi

    Help please!

    Okay, I've no idea how to set the new instance of HttpContext.  The constructor of that class requires some "worker" object as an argument. I don't know what to supply.

  • User profile image
    JohnAskew

    Online help for HttpContext constructor.

    Can you reach this site on the link above?

    Don't ask us me to write your project for you... Tongue Out

  • User profile image
    W3bbo

    mVPstar wrote:
    Help please!

    Okay, I've no idea how to set the new instance of HttpContext.  The constructor of that class requires some "worker" object as an argument. I don't know what to supply.


    You're not meant to create a new instance of HttpContext, it is instantiated by the ASP.NET HTTP pipeline.

    To access the current HttpContext call System.Web.HttpContext.Current.

    Note that the intrinstic Page properties: Request, Response, Server, and the like are all just references to HttpContext.Current.Request/Response.etc. So there is no difference in calling Response.Write("Foo"); or HttpContext.Current.Response.Write("Foo");

  • User profile image
    MasterPi

    Yeah, didn't make sense that you could use Response and Request but had to instantiate HttpContext to use Session.


    EDIT: But if it's in a class library, would ASP.NET instantiate it?

  • User profile image
    bitmask

    Can you put a breakpoint in your code and find out exactly which reference is null?

    HttpContext.Current might be null. You can use HttpContext.Current from a class library, but it has to be on a thread that is currently executing an ASP.NET request.

    HttpContext.Current.Session might be null. I can think of a few reasons why this might happen:

     - Your code is executing in an app that has session state disabled

     - Your code is executing for a Page (or other handler) that has session state disabled.

     - Your code is executing before the pipline initializes session state (i.e. before the AcquireRequestState event).







  • User profile image
    MasterPi

    HttpContext.Current.Session is the part that's null according to VWD.

    I will look into the reasons you listed. 


    I have <pages enableSessionState="true"/> and <sessionState mode="InProc" /> in web.config.  Is that what I should have?

  • User profile image
    bitmask

    Yes, that all looks right. Where does the call into the class library happen? If the call is from inside a web form (like from a Page_Load event handler), then I'm not sure what could be going wrong - should work.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.