Catching the error in global.asax file

  • Thread starter Thread starter Groove
  • Start date Start date
G

Groove

Hey guys - I'm sure this is a commonly asked question but here goes.

I'm trying to catch the error in my global.asax file and redirect to a error
page which will email me the actual error upon page load. Everything seems
to work OK except catch the actual error. It detects the error when I break
the connection string to MS SQL or pause the db itself. The error page is
displayed and I get the email. Only problem is that my session variable
always contains nothing. My code is straight from Murach's ASP.NET 2.0 book
so it makes me think that my issue is environmental.

Any ideas?? Thanks!

My global.asax:

Sub Application_Error(ByVal Sender As Object, ByVal E As EventArgs)


Dim excException As Exception


excException = Server.GetLastError.InnerException

Session("ApplicationError") = excException

Response.Redirect(Application("ApplicationPath") & "errors/500.aspx")


End Sub





My error page:



Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

If Page.IsPostBack = False Then

SendEmail(Session("ApplicationError"))

End If

End Sub





Protected Sub SendEmail(ByVal strError As String)



Dim objMail As New MailMessage

Dim strHTML As String

strHTML = ..etc..... & stError &...etc...

objMail.From = "x"

objMail.To = "y"

objMail.Subject = "....ERROR"

objMail.BodyFormat = MailFormat.Html

objMail.Body = strHTML

SmtpMail.SmtpServer = "z"

SmtpMail.Send(objMail)

End Sub




--
 
Groove,
What I would do here instead of going around in circles, is make the email
method call avaiable right in Global, and call the email method right
after the

excException = Server.GetLastError.InnerException

line. Then you don't need to worry about session state, and the redirect
becomes a non-issue.
Peter
 
Back
Top