473,893 Members | 1,874 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 13740

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
2396
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 variables and then redirects itself to the proper form / procedure depending upon the state of two...
1
3195
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 contents that are intermittently inaccessible. Someone suggested using session variables to...
4
4463
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 here. Any advice would be appreciated. The string being passed is both the variable name and the...
1
1603
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 control output between page loads. (I've pasted what I think is the relevant code at the end of the...
3
3525
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
6078
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 just want to allow one particular session variable to last a long time. Any other suggestions? \
22
14919
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 it session_register("username"); user is taken to the personalized page according to his username...
2
1754
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 minutes in IIS 5.1 by default. I didn't set the timeout as "session.timeout=10" in codes of VBScript
3
3120
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 the test script that I am using: http://php.about.com/od/advancedphp/ss/php_sessions.htm This is...
0
9987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10840
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10929
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10469
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5859
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4684
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3293
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.