I try to setup a E-shop. Next to every item I\'ve an asp:imagebutton
when this imagebutton is clicked I\'m checking whehter the session varialbe session[\"bas
you will be surprised to know the behind the scene the session uses the cache!
have a look at the below(you will find all the common problem about session issue):
http://mmtripathi.blogspot.com/2008/09/session-lost-problem-after.html
http://zeeshanumardotnet.blogspot.com/2009/07/why-sessions-are-terminatedloss.html
http://classicasp.aspfaq.com/general/why-won-t-my-session-variables-stick.html
mesut:
Honestly Session memory can be very volitile. If the application goes down you can loose it, or like you said, if the user's session gets restarted you can loose it. Generally the options for Asp.Net are to store data in:
ViewState (stored as encrypted text accessable by your code-behind)
Session Memory (essentially a dictionary, but as above, a little
volatile)
ASP.Net's State Service (which stores the values in a separate process. watch out for server farm scenarios)
Cookies
Cache
Url Parameters
Each of these methods has a trade-off in terms of performance, memory, or being dependent on a user's environment. I would try a different method and see if that is more robust for you.
Sounds like your appdomain is being recycled. Is your web.config
compilation
element set in debug
mode? In debug mode, ASP.NET resets the appdomain every 15 dynamic compilations. When that happens, if your sessions are stored in memory (InProc
mode - the default), they'll be lost.
FYI, a dynamic compilation will occur whenever a file change happens on your site, like you editing an ASPX file, or an image being generated, or a web.config change.
So either put the site into Release mode (<compilation debug="false" ... />
), or increase the numRecompilesBeforeAppRestart
value from 15 in web.config
, or use a non-volatile session storage mode - e.g. SQL Session State.
Check out whether you are using Application Pool
which is shared across multiple web sites/applications, if you are sharing the same AppPool
with an other web application/site - switch to separate AppPool
.
The best way is to use server-based session state, in this case you can forget about such issues. See HOW TO: Configure SQL Server to Store ASP.NET Session State
Are you losing just that particular variable? - If so, then it is probably some part of your code that is doing this.
If you are losing the entire session object, then unless your session has timed out, the other reason is that if you are using inproc session state at your server and ran out of memory - this recycles your worker process causing you to lose the session value. Check your event log to see if this is the case.