Session Variable in Application_Error

  • Thread starter Thread starter bmukulu
  • Start date Start date
B

bmukulu

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
 
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/
===================================







- 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
 
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.
 
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/
===================================
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/
===================================







- 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
 
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 said:
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.
 
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
 
Back
Top