Advertisement

05.05.2008 at 09:38AM PDT, ID: 23376908
[x]
Attachment Details

Need to direct user to a friendly error page and EMail exception details to the developer.

Asked by Dovberman in Visual Studio .NET 2005, Programming for ASP.NET

Tags: Microsoft, Visual Studio, 2005

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.MailMessage(ToAndFromAddress, 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
 
Loading Advertisement...
 
[+][-]05.05.2008 at 11:56AM PDT, ID: 21502146

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 12:13PM PDT, ID: 21502242

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 01:09PM PDT, ID: 21502596

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 02:18PM PDT, ID: 21503106

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 02:57PM PDT, ID: 21503340

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 03:26PM PDT, ID: 21503530

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.06.2008 at 08:40AM PDT, ID: 21508242

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Visual Studio .NET 2005, Programming for ASP.NET
Tags: Microsoft, Visual Studio, 2005
Sign Up Now!
Solution Provided By: rlively
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628