Why my session variables are getting empty?

  • Thread starter Thread starter ronchese
  • Start date Start date
R

ronchese

I'm noticing this since I started developing a website project: my session variables are getting empty!
I did a real easy test, and I checked my session variable is getting empty!! What is happening?

For example (VS2005, IE6/7, running in filesystem):

- In the Page_load of page1.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Throw New Exception("Error is thrown")
End Sub


- Then, I catch this on global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Current
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Server.GetLastError()
Session("LastException") = ex.Message

'redict to error page
Response.Redirect("../Other/ErrorPage.aspx")
End Sub


- Finally, in the errorpage.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblMessage.Text = Session("LastException") '<-- The session is empty!!
End Sub


There are other situations the same problems also appear. For example, I'm setting an object to session via a shared class and when I gonna get that object again, its empty. In question of less a minute!

Is there any issue with session variables happening in .NET?

Cesar
 
ronchese said:
- Then, I catch this on global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Current
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Server.GetLastError()
Session("LastException") = ex.Message

'redict to error page
Response.Redirect("../Other/ErrorPage.aspx")


Normal request life cycle: Application_AcquireRequestState > Page
Handlers > Application_ReleaseRequestState

Session state restored for persistence at
"Application_AcquireRequestState" and returned to persistence at
"Application_ReleaseRequestState". If you do not capture exception
within page event handler then it interrupts normal request life cycle.
(Session is not saved to persistent storage between requests). When
you call Responce.Redirect you initiate new request without saving
changes made for Session.
So,
1.You can capture exception in page event handler:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//throw exception
}
catch(Exception ex)
{
//process exception
}
}
2.You can use Server.Transfer instead of Responce.Redirect. This method
does not initiate new request, but it trasfers execution to another
page.
3.If you want to use Responce.Redirect then don't save data in Session
but in Cache:
HttpContext.Current.Cache["Error"] = Server.GetLastError();

Sorry, I use C# for examples because I am not good in Visual Basic.
 
Got it, Thanks.
And I got your C# sample, I understand it enough. :^D



marss said:
- Then, I catch this on global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Current
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Server.GetLastError()
Session("LastException") = ex.Message

'redict to error page
Response.Redirect("../Other/ErrorPage.aspx")


Normal request life cycle: Application_AcquireRequestState > Page
Handlers > Application_ReleaseRequestState

Session state restored for persistence at
"Application_AcquireRequestState" and returned to persistence at
"Application_ReleaseRequestState". If you do not capture exception
within page event handler then it interrupts normal request life cycle.
(Session is not saved to persistent storage between requests). When
you call Responce.Redirect you initiate new request without saving
changes made for Session.
So,
1.You can capture exception in page event handler:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//throw exception
}
catch(Exception ex)
{
//process exception
}
}
2.You can use Server.Transfer instead of Responce.Redirect. This method
does not initiate new request, but it trasfers execution to another
page.
3.If you want to use Responce.Redirect then don't save data in Session
but in Cache:
HttpContext.Current.Cache["Error"] = Server.GetLastError();

Sorry, I use C# for examples because I am not good in Visual Basic.
 
Back
Top