How to Access ASP.NET Session State from Application_Error

G

Guest

For one reason or another I am unable to access Session contents in my
asp.net application via the global.asax's Application_Error event. I can
pull this exact code snippet below out of global.asax and place it in a
normal page (or class file that doesn't inherit from system.web.ui.page) and
things work fine:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim currentContext As System.Web.HttpContext =
System.Web.HttpContext.Current
Dim sessionInfo As System.Web.SessionState.HttpSessionState =
currentContext.Session
Response.Write("try to get session count")
Response.Write("<br />")
Response.Write(sessionInfo Is Nothing) ' always returns True
Response.Write("<br />")
Response.Write(sessionInfo.Contents.Count) ' always throws a Null
Reference exception, so would sessionInfo("mySessionVar")
Response.End()
End Sub

Session state is currently being held In Process (default), and it doesn't
matter whether this application has any Session variables established or not
- I get the same result.

Can someone shed some light / provide some working code?
 
G

Guest

Daflookie,
When an unhandled exception occurs in ASP.NET the current session will be
ended and a new session is started - this makes the former session items
unavailable.
--Peter
 
G

Guest

Peter, thanks for the response. I'm hoping I'm hearing you wrong. :)

Are you saying that when an end user experiences an unhandled exception that
user's session state is lost? Similar to recycling the application pool,
only this time for just the user that experienced the error?

For example if an ecommerce module uses sessions to identify what cart ID
the user currently has and they experience an error during checkout the cart
ID held in session would be lost?

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top