473,387 Members | 1,535 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,387 software developers and data experts.

Accessing session variable values from the 'Application_BeginRequest' handler

Stu
Hi,

I am trying to access session variable value from the
'Application_BeginRequest' handler in the global asax file but get the
message - 'Session state not available in this context'. I have tried
loading the current context using 'Dim context As HttpContext =
HttpContext.Current' but get the error 'Object reference not set to an
instance of an object'

Anyone know how you get at this?

Thanks in advance,

Stu

Code:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpContext = HttpContext.Current
If Not IsNothing(context.Session("Test")) Then
context.Session("Test") = "Hello world!"
End If
End Sub
Nov 19 '05 #1
2 5487
Session state isn't loaded until later in the lifetime of the request. Instead
of Application_BeginRequest, build Application_PreRequestHandlerExecute --
this is the safest place to do your work prior to the page getting called.
The step where session is actually loaded in Application_AcquireRequestState,
so that's another option. Again, these are all in global.asax.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

I am trying to access session variable value from the
'Application_BeginRequest' handler in the global asax file but get the
message - 'Session state not available in this context'. I have tried
loading the current context using 'Dim context As HttpContext =
HttpContext.Current' but get the error 'Object reference not set to an
instance of an object'

Anyone know how you get at this?

Thanks in advance,

Stu

Code:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
Dim context As HttpContext = HttpContext.Current
If Not IsNothing(context.Session("Test")) Then
context.Session("Test") = "Hello world!"
End If
End Sub


Nov 19 '05 #2
Stu
Thanks - that worked great.

Stu

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:51**********************@msnews.microsoft.com ...
Session state isn't loaded until later in the lifetime of the request.
Instead of Application_BeginRequest, build
Application_PreRequestHandlerExecute --
this is the safest place to do your work prior to the page getting called.
The step where session is actually loaded in
Application_AcquireRequestState, so that's another option. Again, these
are all in global.asax.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

I am trying to access session variable value from the
'Application_BeginRequest' handler in the global asax file but get the
message - 'Session state not available in this context'. I have tried
loading the current context using 'Dim context As HttpContext =
HttpContext.Current' but get the error 'Object reference not set to an
instance of an object'

Anyone know how you get at this?

Thanks in advance,

Stu

Code:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
Dim context As HttpContext = HttpContext.Current
If Not IsNothing(context.Session("Test")) Then
context.Session("Test") = "Hello world!"
End If
End Sub


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
3
by: Paul Daly (MCP) | last post by:
I'm trying to write a log file that captures the referring url if the request is a new session, and captures a querystring value if the user is browsing between pages on the website. When using...
5
by: news.microsoft.com | last post by:
Hi everyone, I need some help (may be in the form of some sample code) for the subject question. I have an ASP.NET/C# application. I need to do quite a few tasks when the session ends. I...
2
by: David Berman | last post by:
It seems that my site is losing session information when using Server.Transfer. I have a page called PictureGallery.aspx. It takes an argument which is an index id, so it would look like...
10
by: Nemisis | last post by:
Hi everyone, I am trying to create a custom error page, that the user gets shown when a error has occurred within my website. The code works perfectly, apart from when an invalid URL is typed...
5
by: aamirghanchi | last post by:
Hi, I need to know if anyone else came across this. The Session variable value I set in a sortCommand event handler of a datagrid does not hold on till the next sortcommand event handler and...
2
by: sony.m.2007 | last post by:
Hi, When i try to set a value for a session variable I'm getting a object refence not set error I tried two methods as below HttpContext.Current.Session.Add("AppStartTime", DateTime.Now);...
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: 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?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.