473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 13587

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Al Jones | last post by:
This is a repost form the vbscript newgroup - if this isn't the appropriate group would you point me toward one that is. Basically, I seem to be losing session data part way though preparing an...
1
by: Steve Remer | last post by:
My application (relevant code snippets below) originally used Session variables in order to maintain state from page to page. After being unable to solve the mystery of why those variables were...
4
by: Amy | last post by:
I'm having a problem with clearing my session variables. I'm using session variables to pass data from a web form to a pdf generator. The problem comes in when a user goes back to the original...
1
by: Tom | last post by:
I've been researching this for a while now -- perhaps someone here knows the explanation. I uploaded to my remote host (system info: PHP Version 4.3.11, FreeBSD metis.cleverdot.com...
3
by: Lee Moore | last post by:
I have some user controls defined that represent a common header and footer for a particular site. the footer contains links with querystring parameters based on session variables. The problem is,...
6
by: javelin | last post by:
In ASP classic pages, I want to know if it's possible to prevent session variables from becoming zero length strings? I have tried setting the Session.Timeout to a large value, but alwas, after 20...
22
by: K. A. | last post by:
I have two servers at work, 'A' for testing and development, and server 'B' for production. On server A, I wrote a PHP test code to login users then direct them to a personalized page. This is...
2
by: Orgil | last post by:
I'm using ASP 3.0 however there is ASP.NET, because I'm working an old site that is built in ASP 3.0. I hope you for getting any help for my problem from you. So, my site's sessions are empty...
3
by: mikeboston | last post by:
Hi, I am attempting to use php session variables on a server which is running Red Hat Linux, but the variables don't seem to be getting passed between pages. I have tried the same exact test...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.