Monday, March 26, 2012

Session Variable in Application_Error

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 advancePost the code which is failing.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.com> wrote in message news:1174846749.292871.264970@.y66g2000hsf.googlegroups
.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
>
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=F1ol :http://asp.net.do/foros/
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3Ddarkred">
>
> <bmuk...@.gmail.com> wrote in messagenews:1174846749.292871.264970@.y66g200=
0hsf.googlegroups.com...
>
>
> - 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 =3D Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") =3D "This is an
example"
End If
End Sub
bmukulu@.gmail.com wrote:
> On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
> wrote:
> 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.
Gran Andersson
_____
http://www.guffa.com
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: " + e
xcept.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 informatio
n 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 espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.com> wrote in message news:1174849512.334478.233980@.n59g2000h
sh.googlegroups.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 espaol :http://asp.net.do/foros/
> ===================================
>
> <bmuk...@.gmail.com> wrote in messagenews:1174846749.292871.264970@.y66g2000
hsf.googlegroups.com...
>
>
> - 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
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
"bmukulu@.gmail.com" wrote:

> On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
> wrote:
> 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
>
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:

> bmukulu@.gmail.com wrote:
> 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
>
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:
> If you read the original post again, you'll see that this is exactly
> what he is getting.
>
> --
> G?ran Andersson
> _____
> http://www.guffa.com
>
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

0 comments:

Post a Comment