473,606 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Session Variable in Application_Err or

Hi,

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?

Thanks in advance

Mar 25 '07 #1
8 2564
Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== =====
<bm*****@gmail. comwrote in message news:11******** **************@ y66g2000hsf.goo glegroups.com.. .
Hi,

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?

Thanks in advance

Mar 25 '07 #2
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@ nowhere.com>
wrote:
Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en español :http://asp.net.do/foros/
=============== =============== =====

<bmuk...@gmail. comwrote in messagenews:11* *************** ******@y66g2000 hsf.googlegroup s.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advance- Hide quoted text -

- Show quoted text -
Here is the code:
Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso TypeOf
ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an
example"

End If

End Sub

Mar 25 '07 #3
bm*****@gmail.c om wrote:
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@ nowhere.com>
wrote:
>Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en español :http://asp.net.do/foros/
============== =============== ======

<bmuk...@gmail .comwrote in messagenews:11* *************** ******@y66g2000 hsf.googlegroup s.com...
>>Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advance- Hide quoted text -
- Show quoted text -

Here is the code:
Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso TypeOf
ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an
example"

End If

End Sub
You have to check if you have an HttpContext object before you try to
use it. It's not at all certain that the code where the error occurs has
a http context at all.

--
Göran Andersson
_____
http://www.guffa.com
Mar 25 '07 #4
Can you try this and see if it makes a difference ?

Protected Sub Application_Err or(sender As [Object], e As EventArgs)

' Get the information about the error
Dim ctxt As HttpContext = HttpContext.Cur rent

Dim except As Exception = ctxt.Server.Get LastError()

Dim errorInfo As String = "<br>Offend ing URL: " + ctxt.Request.Ur l.ToString() + "<br>Source : " _
+ except.Source + "<br>Messag e: " + except.Message + "<br>Stack trace: " + except.StackTra ce

ctxt.Response.W rite(errorInfo)

' --------------------------------------------------
' To let the page finish executing we clear the error
' --------------------------------------------------
ctxt.Server.Cle arError()
End Sub

--------

Next suggestion :

Without a valid session, you won't be able to store the exception information in it.

Try wrapping the statement in a conditional :

If Not HttpContext.Cur rent.Session Is Nothing Then
Dim ctxt As HttpContext = HttpContext.Cur rent
Dim except As Exception = ctxt.Server.Get LastError()
HttpContext.Cur rent.Session.Ad d("sExample", except)
End If

That will tell you if your Session is valid.

If it is valid, you'll see the Session sExample set with the Exception info.
If it's not valid, Session sExample will be empty.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== =====
<bm*****@gmail. comwrote in message news:11******** **************@ n59g2000hsh.goo glegroups.com.. .
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@ nowhere.com>
wrote:
Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en español :http://asp.net.do/foros/
=============== =============== =====

<bmuk...@gmail. comwrote in messagenews:11* *************** ******@y66g2000 hsf.googlegroup s.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advance- Hide quoted text -

- Show quoted text -
Here is the code:
Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso TypeOf
ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an
example"

End If

End Sub
Mar 25 '07 #5
Howdy,

You have to call Server.ClearErr or method after handling Application.OnE rror
event in order to prevent thread from being aborted (unhandled exception).
Without doing so, ASP.NET runtime assumes exception has not been handled and
stops the execution before session state is updated with chages.

Sub Application_Err or(ByVal sender As Object, _
ByVal e As EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso _
TypeOf ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an example"
Server.ClearErr or()
End If

End Sub

hope this helps

--
Milosz
"bm*****@gmail. com" wrote:
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@ nowhere.com>
wrote:
Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en español :http://asp.net.do/foros/
=============== =============== =====

<bmuk...@gmail. comwrote in messagenews:11* *************** ******@y66g2000 hsf.googlegroup s.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advance- Hide quoted text -
- Show quoted text -

Here is the code:
Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso TypeOf
ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an
example"

End If

End Sub

Mar 28 '07 #6
Hi Goran,

If HttpContext.Cur rent was null he would get null reference exception. The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearErr or afterwards.

Best regards
--
Milosz
"Göran Andersson" wrote:
bm*****@gmail.c om wrote:
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@ nowhere.com>
wrote:
Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en español :http://asp.net.do/foros/
=============== =============== =====

<bmuk...@gmail. comwrote in messagenews:11* *************** ******@y66g2000 hsf.googlegroup s.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Err or procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advance- Hide quoted text -
- Show quoted text -
Here is the code:
Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastE rror()
If Not ex.InnerExcepti on Is Nothing AndAlso TypeOf
ex.InnerExcepti on Is System.IO.FileN otFoundExceptio n Then
HttpContext.Cur rent.Session("s Example") = "This is an
example"

End If

End Sub

You have to check if you have an HttpContext object before you try to
use it. It's not at all certain that the code where the error occurs has
a http context at all.

--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #7
Milosz Skalecki [MCAD] wrote:
Hi Goran,

If HttpContext.Cur rent was null he would get null reference exception.
If you read the original post again, you'll see that this is exactly
what he is getting.
The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearErr or afterwards.

Best regards
--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #8
Good call Goran, your're right. I thought he could not read the information
already stored- I must have been very sleepy at the time i replied :)

Regards
--
Milosz
"Göran Andersson" wrote:
Milosz Skalecki [MCAD] wrote:
Hi Goran,

If HttpContext.Cur rent was null he would get null reference exception.

If you read the original post again, you'll see that this is exactly
what he is getting.
The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearErr or afterwards.

Best regards

--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #9

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

Similar topics

1
5909
by: Greg Burns | last post by:
I am trying to write a global custom error page. I thought I would jot down some of what I've learned so far... At first I just used the default customErrors section with a defaultRedirect tag, as such: <customErrors defaultRedirect="error.aspx" mode="On" /> First thing I found out about this method, is that I didn't have access to the exception object in my error.aspx page. So this page was going to be
9
2447
by: William LaMartin | last post by:
I have a problem, mentioned here before, of Session and Application variables disappearing at one site but not at others or on my development computer. The problem is illustrated by an example at http://www.lamartin.com/dotnet/sessiontestset.aspx, were I set Session, Application and Cache variables on the first page and then on the second page view them as the second page is refreshed every five seconds. Before 10 refreshes, the...
1
2110
by: Michael | last post by:
Hello, In my handled exceptions (using try/catch), I would like to be able to log certain variable values when an error occurs, to make debugging easier. I'm using the sub application_error in global.asax to output to a log file every time an error occurs. It would be easy for me to assign session variables in my catch blocks, that are set to variable values at the time of errors.
3
3209
by: Mike Malter | last post by:
I have a general error page that I configured in web.config as <customErrors mode="On" defaultRedirect="CRDefaultError.aspx" /> This error page comes up whenever an error occurs outside of any try/catch blocks. This page has a text box in it that I would like to fill with relevant information about the error. I am trying to put the Server.GetLastError().ToString() in there. It did not work on the page, and I read somewhere that I should...
1
4931
by: Jon Paugh | last post by:
Hi, So in Application_Error method in Global class of my ASP.NET web project, I add: HttpContext.Current.Session = "SomeSessionThing"; Then, in Application_Error method I Response.Redirect to a error page. In the page's load, I check the value of
8
1989
by: tshad | last post by:
I have an Application_Error function in my Global.asax function that works fine until I try to access my Session variables. I am emailing the results to myself whenever I get an error and would like to get the list of the users Session Variables there were there at the time of the error. But if I add the following line: MyMessage.Body &= "Session Variable" & session("JobTitle") & vbCrLf it goes back to the MS error page instead of...
4
14468
by: BenCoo | last post by:
In my ASP.NET 2.0 application I have in the global.asax.file the following code Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when an unhandled error occurs This dousn't work !!! Get error "Session state is not available in this context." Dim ex As Exception ex = Server.GetLastError.InnerException Session("Exception") = ex
14
2244
by: Rick | last post by:
We are in the process of testing a large web project that I converted from VS 2003 to VS 2005. Everything seems to be working except for a few minor things. But the main issue I have is this, I have about 5 or 6 developers testing this web site in a staging environment on a Microsoft 2003 Server box. We have a base page that gets called on every page and checks for session variables. After about 20 - 30 minutes these session variables are...
13
3045
by: SAL | last post by:
Okay, don't bash me to hard for my design on this app, it's my first web app and it's in production. My basic design is using Datatables created via the designer with a business logic class in between the datatable and ObjectDataSources. In one page I had a Gridview with select enabled. When an row in the grid is selected, I retrieve the SelectedValue, store the value in a Session variable and redirect the response to another web page,...
0
8431
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
8096
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
6773
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3937
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
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1300
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.