471,579 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

Why my session variables are getting empty?

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
2 13114

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
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.

Similar topics

4 posts views Thread by Amy | last post: by
1 post views Thread by Tom | last post: by
3 posts views Thread by Lee Moore | last post: by
22 posts views Thread by K. A. | last post: by
2 posts views Thread by Orgil | last post: by
3 posts views Thread by mikeboston | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.