By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,589 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,589 IT Pros & Developers. It's quick & easy.

How to Access ASP.NET Session State from Application_Error

P: n/a
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?
Mar 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Daflookie" wrote:
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?

Mar 4 '06 #2

P: n/a
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
"Peter Bromberg [C# MVP]" wrote:
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Daflookie" wrote:
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?

Mar 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.