Sean's Personal .NET Code Samples And References

ASP.NET error notification in Visual Basic

This is pretty handy; this is the best way to know about all the errors that occur on a site.

You can’t rely on the users to let you know as they may not know what they are talking about.

I know when I’m on a site that crashes, I don’t bother trying to get a hold of the webmaster to inform them their sites broken.

I just leave with a bad experience; possibly to never return.

Here is the code for the Global.asax file.

'IMPORT System.Net.Mial FOR SENDING THE NOTIFICATION
<%@ Import Namespace="System.Net.Mail"%>

'This goes in the Global.asax Application_Error
    'CODE THAT RUNS WHEN AN UNHANDLED ERROR OCCURES

'DECLARE AND SET THE EXCEPTION
Dim ex As Exception = Server.GetLastError().GetBaseException()

If ex IsNot Nothing Then
Try
    'DECLARE THE ERROR STRING TO MAIL
    Dim err As String = ""
    
    'ADD THE ERRROR TYPE
    If TypeOf ex Is System.Data.SqlClient.SqlException Then
        err = "<h3>SQL Databse Error</H3>"
    ElseIf TypeOf ex Is ApplicationException Then
        err = "<h3>Application Exception</H3>"
    Else
        err = "<h3>Unknown Error</H3>"
    End If

    'ADD THE USER INFORMATION TO THE ERROR STRING
    err += "<h3><u>USER INFORMATION</u></h3>"
    err += "<br />Remote Address: " & Request.ServerVariables("REMOTE_ADDR").ToString
    err += "<br />Remote Server: " & Request.ServerVariables("REMOTE_HOST").ToString
    err += "<br />Users Browser: " & Request.ServerVariables("HTTP_USER_AGENT").ToString
    
    'USEING A TRY SO THIS WILL STILL WORK IF THE ADDRESS IT TYPED IN THE ADDRESS BAR
    Try
        err += "<br />Reffered From: " & Request.ServerVariables("HTTP_REFERER").ToString
    Catch
        err += "<br /><b>NO REFERER</b> ~ URL Direct Access"
    End Try
    
    'ADD THE ERROR INFORMATION TO THE ERROR STRING
    err += "<h3><u>ERROR INFORMATION</u></h3>"
    err += "<br />Time of Error: " & Now.ToString
    err += "<br />Error Page: " & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")
    err += "<br />Error Caught in Application_Error event" & _
              "<br />Error Message: " & ex.Message.ToString() & _
              "<br /><br />Stack Trace:" & ex.StackTrace.ToString()

    'SET UP THE EMAIL AND ADD THE ERROR STRING TO THE BODY
    
    'SENDING THE ERROR TO USING THE DEFAULT FORM IN THE web.config
    Dim ToAddress As String = "WHERE YOU WANT TO RECIEVE THE NOTIFICATION.com"
    
    'DECLARE THE MAIL MESSAGE AND ADD THE TO ADDRESS
    Dim message As New MailMessage
    message.To.Add(New MailAddress(ToAddress))

    'SET THE EMAIL SUBJECT
    message.Subject = "An unhandled exception occurred!"
    
    'SET THE EMAIL BODY
    message.Body = err
    message.IsBodyHtml = True

    'SEND EMAIL TO FROM ADDRESS IF THE DELIVERY FAILS
    message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
    
    'DECLARE THE MAIL SMTP CLIENT
    Dim mailClient As SmtpClient = New SmtpClient

    'SEND THE EMAIL AND CLEAN UP THE MESSAGE
    mailClient.Send(message)
    message.Dispose()
Catch mailex As Exception
    'OOPS, A PROBLEM SENDING THE MAIL!
    'JUST SEND THE USER TO THE CUSTOM ERROR PAGE...
End Try
End If ' ex IsNot Nothing Then
'IF NOT SENDING THE USER TO A CUSTOM ERROR PAGE THEN CLEAR THE ERROR
'Server.ClearError()
Sean Marcellus
There are 10 kinds of people e in this world, those who understand binary and those who don’t.