By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,979 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,979 IT Pros & Developers. It's quick & easy.

Session Variable in Application_Error

P: n/a
Hi,

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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
Share this Question
Share on Google+
8 Replies


P: n/a
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.googlegr oups.com...
Hi,

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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

P: n/a
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**********************@y66g2000hsf.g ooglegroups.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub

Mar 25 '07 #3

P: n/a
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**********************@y66g2000hsf.g ooglegroups.com...
>>Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "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

P: n/a
Can you try this and see if it makes a difference ?

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

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

Dim except As Exception = ctxt.Server.GetLastError()

Dim errorInfo As String = "<br>Offending URL: " + ctxt.Request.Url.ToString() + "<br>Source: " _
+ except.Source + "<br>Message: " + except.Message + "<br>Stack trace: " + except.StackTrace

ctxt.Response.Write(errorInfo)

' --------------------------------------------------
' To let the page finish executing we clear the error
' --------------------------------------------------
ctxt.Server.ClearError()
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.Current.Session Is Nothing Then
Dim ctxt As HttpContext = HttpContext.Current
Dim except As Exception = ctxt.Server.GetLastError()
HttpContext.Current.Session.Add("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.googlegr oups.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**********************@y66g2000hsf.g ooglegroups.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub
Mar 25 '07 #5

P: n/a
Howdy,

You have to call Server.ClearError method after handling Application.OnError
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_Error(ByVal sender As Object, _
ByVal e As EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso _
TypeOf ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an example"
Server.ClearError()
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**********************@y66g2000hsf.g ooglegroups.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub

Mar 28 '07 #6

P: n/a
Hi Goran,

If HttpContext.Current 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.ClearError afterwards.

Best regards
--
Milosz
"G├Âran Andersson" wrote:
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**********************@y66g2000hsf.g ooglegroups.com...
Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error 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_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "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

P: n/a
Milosz Skalecki [MCAD] wrote:
Hi Goran,

If HttpContext.Current 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.ClearError afterwards.

Best regards
--
G├Âran Andersson
_____
http://www.guffa.com
Mar 28 '07 #8

P: n/a
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.Current 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.ClearError afterwards.

Best regards

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

This discussion thread is closed

Replies have been disabled for this discussion.