473,586 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles Me.Load
Throw New Exception("Erro r is thrown")
End Sub
- Then, I catch this on global.asax:
Sub Application_Err or(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Cur rent
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Serv er.GetLastError ()
Session("LastEx ception") = ex.Message

'redict to error page
Response.Redire ct("../Other/ErrorPage.aspx" )
End Sub
- Finally, in the errorpage.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
lblMessage.Text = Session("LastEx ception") '<-- 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 13665

ronchese (a) wrote:
- Then, I catch this on global.asax:
Sub Application_Err or(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Cur rent
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Serv er.GetLastError ()
Session("LastEx ception") = ex.Message

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

Normal request life cycle: Application_Acq uireRequestStat e Page
Handlers Application_Rel easeRequestStat e

Session state restored for persistence at
"Application_Ac quireRequestSta te" and returned to persistence at
"Application_Re leaseRequestSta te". 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.Redire ct you initiate new request without saving
changes made for Session.
So,
1.You can capture exception in page event handler:
protected void Page_Load(objec t sender, EventArgs e)
{
try
{
//throw exception
}
catch(Exception ex)
{
//process exception
}
}
2.You can use Server.Transfer instead of Responce.Redire ct. This method
does not initiate new request, but it trasfers execution to another
page.
3.If you want to use Responce.Redire ct then don't save data in Session
but in Cache:
HttpContext.Cur rent.Cache["Error"] = Server.GetLastE rror();

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.netw rote in message
news:11******** **************@ 73g2000cwn.goog legroups.com...
>
ronchese (a) wrote:
>- Then, I catch this on global.asax:
Sub Application_Err or(ByVal sender As Object, ByVal e As EventArgs)
'get current context
Dim ctxContext As HttpContext = HttpContext.Cur rent
If ctxContext Is Nothing Then Exit Sub

'get last error
Dim ex As Exception = ctxContext.Serv er.GetLastError ()
Session("LastEx ception") = ex.Message

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


Normal request life cycle: Application_Acq uireRequestStat e Page
Handlers Application_Rel easeRequestStat e

Session state restored for persistence at
"Application_Ac quireRequestSta te" and returned to persistence at
"Application_Re leaseRequestSta te". 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.Redire ct you initiate new request without saving
changes made for Session.
So,
1.You can capture exception in page event handler:
protected void Page_Load(objec t sender, EventArgs e)
{
try
{
//throw exception
}
catch(Exception ex)
{
//process exception
}
}
2.You can use Server.Transfer instead of Responce.Redire ct. This method
does not initiate new request, but it trasfers execution to another
page.
3.If you want to use Responce.Redire ct then don't save data in Session
but in Cache:
HttpContext.Cur rent.Cache["Error"] = Server.GetLastE rror();

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
2380
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 email from (possibly) three seperate forms. the following code is the end of a routine which stashes data from the first form off to session...
1
3169
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 intermittently inaccessible, (all Session variables gone, not using InProc Session state mode), I moved to a cookies-based solution. Now I have cookie...
4
4450
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 page and takes data out of the field - the session variable is persisting. i tried the following, and it didn't work - I'm not sure where to go from...
1
1592
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 5.3-RELEASE-p13 FreeBSD 5.3-RELEASE-p13 #0: kernbuild@kernbuild.kgix.net:/usr/obj/usr/src/sys/KGIX-SMP-PMAP i386) a script that uses $_SESSION variables to...
3
3508
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, I cannot access the session variables within my user control. The user controls work perfectly otherwise. Example code included.
6
6059
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 minutes, my session variable times out. I also tried setting the session timeout in IIS manager to a high value, but this did no good either. I...
22
14868
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 done in 3 steps: Step 1. Normal http login page. Step 2. A page called login.php that takes the posted username, stores it as $_SESSION, and registers...
2
1740
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 before its timeout. I have searched and checked for long time around this problem, but I can't fix the problem yet. The session timeout is 20...
3
3106
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 script on my third party webhost, and it works just fine. Does anyone know of anything that needs to be changed in a default Red Hat php setup? This is...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.