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

Why my session variables are getting empty?

P: n/a
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


Dec 24 '06 #1
Share this Question
Share on Google+
2 Replies

P: n/a

ronchese (a) wrote:
- 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.

Dec 25 '06 #2

P: n/a
Got it, Thanks.
And I got your C# sample, I understand it enough. :^D

"marss" <ma***@ukr.netwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
>
ronchese (a) wrote:
>- 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.

Dec 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.