When an error occurs, the friendly error page, errorpage.aspx needs to open. A detailed exception message needs to be Emailed to the developer.
I copied code from references. The global.aspx handles all errors. However the friendly error page does not open. I, the developer am not receiving an error detail mail message.
What am I missing?
This is what I have:
web.config
<customErrors mode="RemoteOnly" defaultRedirect="ErrorPage
.aspx">
</customErrors>
</system.web>
<system.net>
<mailSettings>
<smtp from="myname@mailsite.com"
>
<network
host="localhost"
password=""
userName=""/>
</smtp>
</mailSettings>
</system.net>
</configuration>
ErrorPage.aspx.vb
Imports System.Net.Mail
Imports System.Diagnostics
Partial Class ErrorPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objClient As New SmtpClient
Dim objEMail As New MailMessage
Dim strTo As String = ""
Dim strUserName As String = ""
Dim usrCurrent As MembershipUser = Membership.GetUser()
Dim strErrorMessage As String = ""
If Not usrCurrent Is Nothing Then
strUserName = usrCurrent.UserName
strErrorMessage = "Thank you:" & strUserName _
& " The error has been Emailed to our development staff."
End If
txtErrorMsg.Text = strErrorMessage
End Sub
End Class
Global.asax
<%@ Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
'Get exception details
' Give the user some information, but
' stay on the default page
Dim ex As Exception = Server.GetLastError()
Response.Write("<h2>Global
Page Error</h2>" & Chr(10) & "")
Response.Write("<p>" + ex.Message + "</p>" & Chr(10) & "")
Response.Write("Return to the <a href='Default.aspx'>" + "Default Page</a>" & Chr(10) & "")
If ex IsNot Nothing Then
Try
MsgBox("Error Detected", MsgBoxStyle.Information, "Error")
'Email the administrator with information that an error has occurred
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAndFromAddress As String = "david.martin@fema.gov"
'(1) Create the MailMessage instance
Dim msgError As New System.Net.Mail.MailMessag
e(ToAndFro
mAddress, ToAndFromAddress)
'(2) Assign the MailMessage's properties
msgError.Subject = "An unhandled exception occurred!"
msgError.Body = String.Format("An unhandled exception occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}", _
System.Environment.NewLine
, ex.Message, ex.StackTrace)
msgError.IsBodyHtml = False
'(3) Create the SmtpClient object
Dim smtp As New System.Net.Mail.SmtpClient
'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(msgError)
Catch
'Whoops, some problem sending email!
'Just send the user onto ErrorPage.aspx...
End Try
End If
' Clear the error from the server
Server.ClearError()
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
</script>
Start Free Trial