Session value becomes null across action method in same controller in mvc RRS feed

  • Question

  • User-129908252 posted

    I'm saving the list object into session like this In action method

    HttpContext.Session["AdditionalShippingAddress"] = resultobj;

    Another post action while accessing getting null

    List<AdditionalShippingAddressModel> shippingAddressList = (List<AdditionalShippingAddressModel>)HttpContext.Session["AdditionalShippingAddress"];

    I don't know why it is happening.

    Please suggest me.

    Thanks In Advance.

    Thursday, December 15, 2016 10:58 AM

All replies

  • User475983607 posted

    Did you make sure the data is not null to begin with?  

    Other than that, you're describing the danger of using InProc Session without checking for null.  

    Keep in mind, storing items in Session is the same as storing data in two different places; Session and the Database.  The database is typically and should be the system of record. Therefore, if Session is null for any reason you should reload Session from the system of record - the database.  This is a common pattern when using session or any form of secondary persistence. 

    Thursday, December 15, 2016 12:10 PM
  • User-271186128 posted

    Hi jagan12013,

    First, as mgebhard said, please set a break point and check whether the resultobj is null or not.

    Second, perhaps the session expired, please make sure it not exceeded the expiry time.

    Finally, you could check the Session-State Modes, if you are using InProc mode, the data might loss. You could try to use SQLServer mode. More details, please refer to this article:


    Best regards,

    Friday, December 16, 2016 6:19 AM