Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 10, 2012 03:30 PM by nelson123
Aug 07, 2012 08:49 PM|LINK
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
Aug 07, 2012 08:50 PM|LINK
Use a database or some other state management approach.
Aug 09, 2012 07:23 AM|LINK
Hi, based on your requirement, I suggest using Cookie:
Aug 09, 2012 08:50 PM|LINK
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:
Aug 10, 2012 12:15 AM|LINK
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.
Aug 10, 2012 12:42 AM|LINK
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.
Aug 10, 2012 07:41 AM|LINK
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:
Aug 10, 2012 03:30 PM|LINK
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