Persist Session Value or Replacement For Session RRS feed

  • Question

  • User1887777618 posted


    i have a code in which i store some values in the session variable. my session timeout is set to 30 minutes.

    now after 30 minutes the value is lost from the session variable if the page is left unused for 30 minutes. and when trying to access the value, the value returned is null.

    now i dont want this to happen even after for a long period of time, i want that value should remain there. Is there an alternative to this.

    note: the session is used across multiple pages


    Tuesday, August 7, 2012 4:49 PM


All replies

  • User1779161005 posted

    Use a database or some other state management approach.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2012 4:50 PM
  • User1954304945 posted

    Hi, based on your requirement, I suggest using Cookie:


    Thursday, August 9, 2012 3:23 AM
  • User-120949784 posted


    As suggested above cookies are best way for your requirement but cookies come with size limitation. Dont really know what you are trying to achieve but consider using Application object too.  You can save the value in application object. The value stored in applicaton object remains in the memory till the application is running.

    But please go through the following link before working with application objects:



    Thursday, August 9, 2012 4:50 PM
  • User-1438829842 posted

    M.Kamran Azam, aoa,

    I'll prefer you to use Database. Store value in Database and read from there,, and also you can use cookie is ideal for this situation..

    Or Im giving you an idea.. about HiddenField on every page. to restore the Session

    Store Value in Session also in HiddenField.

    if(Session["yourKey"] == null)


     Session["yourKey"] = HiddenField1.Value;


    when ever session will be null it will restore from HiddenField.

    So, you have 3 ways Cookies, DB, Session/HiddenField Merge.

    Good Luck`

    Thursday, August 9, 2012 8:15 PM
  • User1887777618 posted

    thanks all for your replies, but i have already gone through all these stuffs.

    let me explain wat i am tryin to achieve. Its pretty long, but i will go by it now.

    i am making a shopping cart.

    now the usage....

    user(guest user or anonymous) comes online, select products, adds it to cart, the product is added to cart and the cart detail is stored in the DB along with the unique key identifying this particular user and his cart items. all other user's cart items are differentiated based upon this particular unique id of the user taken from Request.AnonymousUserID property.

    so every time the cart items are fetched i get only those rows from the table which have this AnonymousUserID.

    now everything works fine, but when the site is left for few minutes, untouched or unused, the AnonymousUserID is lost. and the application is unable to determine which user is accesing the webpage and hence is unable to retrieve cart items from the Cart table based upon the UniqueID which this user was assigned when he first visited the site. This creates a problem and the user loses all his cart items one he lefts the application for few minutes.

    How i am currently evading it, is by setting the Application Pool Idle Timeout on the server to 20 minutes, hence the UniqueID is maintained upto 20 minutes, but not more. if i set it to 60 minutes, it is maintained upto 60 minutes as the pool is not recycled.

    this is causing resource problem on server, and i dont want to do it, so what can i do to make it work, coz i see on many sites that the Guest cart remains for very long time even if the site is left untouched for hours. what am i missing.

    Thursday, August 9, 2012 8:42 PM
  • User1954304945 posted

    Hi, with your requirement, it is better to use cookie, you can set the cookie expire time to a long time, for example: one month. In this way, the cookie will persists during this month. There is an example on the following link, you can refer to it:


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2012 3:41 AM
  • User400339653 posted

    you can dissable the session time out from web.config or global.asa or

    You can use different objects of session management like application object,cache object, database,cookies etc.

    Mark as answer if you find the post useful

    Friday, August 10, 2012 11:30 AM