Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variable is being cleared?

Status
Not open for further replies.

fr2

Programmer
May 13, 2008
45
US
I set a customer error page redirect in my web.config ..

<system.web>
<customErrors mode="On" defaultRedirect="ErrorPage.aspx" >
</customErrors>
</system.web>

Then in the global.asax
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Session("LastError") = Server.GetLastError.ToString()
End Sub


when I debug the app ... Session("LastError") is set in app_error event of the global.asax,
but when I go to retrieve it in the Errorpage.aspx .. (to send it in an email ) .. it is set to NOTHING .. why?
 
Get your last error this way:

Dim ex As Exception = Server.GetLastError.GetBaseException

Then you could make things really fancy:

Code:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        Dim ex As Exception = Server.GetLastError.GetBaseException
        
        CommonFunctions.WriteOutException("C:\Error.Log", ex, Request.QueryString.ToString, Request.Url.ToString _
            , Request.UserHostAddress.ToString, HttpContext.Current.Server.MachineName, HttpContext.Current.Request.UrlReferrer.OriginalString)
        
        Dim sbEmail As New StringBuilder
        
        If ex.Message.Trim <> "" Then
        
            ' Build the error text
            sbEmail.Append("<div style='color:Navy;font:normal normal bold 12pt Palatino Linotype;'>An error has occurred on the " & ConfigurationManager.AppSettings("ApplicationName") & " Site.</div><br/><br/>")
            
            If Not HttpContext.Current.Request.Url Is Nothing Then
                sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The error occurred in: </b> " & HttpContext.Current.Request.Url.ToString & "</div>")
            End If
            
            sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The Client's IP is: </b><i>" & Request.UserHostAddress.ToString & "</i></div>")
            sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The Web Server is: </b><i>" & HttpContext.Current.Server.MachineName & "</i></div>")
            sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The Previous Page was: </b><i>" & HttpContext.Current.Request.UrlReferrer.OriginalString & "</i></div><br/>")
            
            If Not ex.Message Is Nothing Then
                sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The message was: </b><i>" & ex.Message.ToString & "</i></div><br/>")
            End If
            
            If Not ex.StackTrace Is Nothing Then
                sbEmail.Append("<div style='color:Maroon;font:normal normal normal 10pt Palatino Linotype;'><b>The stack trace was: </b> " & ex.StackTrace.ToString.Replace("at ", "<br/>at ") & "</div><br/>")
            End If
            
            ' send the email
            CommonFunctions.SendEmail("mailbox@server.com", "Error", "tomailbox@server.com" _
                , "An error has occurred on the " & ConfigurationManager.AppSettings("ApplicationName") & " Site", sbEmail.ToString _
                , Net.Mail.MailPriority.High)
        End If
        
        'Server.Transfer("GeneralError.aspx", True)
        'Server.ClearError()
        'System.Diagnostics.EventLog.WriteEntry("HWC Web Application", ErrMsg, Diagnostics.EventLogEntryType.Error)
        'response.Redirect("HWC_Error.aspx")
    End Sub

* Sine scientia ars nihil est
* Respondeat superior
 
You see .. the more u give .. the more they want .. I provided the CLient IP address as you had it

sbEmail.Append("<div style='color:black;font:normal normal normal 10pt Palatino Linotype;'><b>The Client's IP is: </b><i>" & Request.UserHostAddress.ToString & "</i></div>")

Now they are asking if they can have the logged in User. Is there a simple way to get the windows or domain logged in user name ...

I tried ..
strUser = Request.LogonUserIdentity.ToString()
strUSer = Request.UserHostName.ToString()

but I dont think that is it?
 
Request.User.Identity.Name or something like that. this will give you the user for the current thread. if you're using Windows Authentication then it should match WindowsIdentity.GetCurrent().Name.

I would also recommend seperating the email formatting and data as it would be much easier to read. Something Like
string body FormatEmailBody(Server.GetLastError);

You get the full exception just using exception.ToString(); this will include the message, stack trace and inner exception.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
how or where do you use FormatEmailBody(ex.ToString()) to format the body of the email ... i get "Name FormatEmailBody is not declared" ... Is FormatEmailBody a builtin function?

Dim sendTo(4) As String
Dim strSubject As String
Dim sbEmail As New StringBuilder

sbMail.Append(ex.ToString)

Dim arrUserInfo As New ArrayList


Dim MailObj As New SmtpClient()
Dim MailMsg As MailMessage
Dim emailString As String

'Try
MailMsg = New MailMessage()

For Each emailString In sendTo
If Not String.IsNullOrEmpty(emailString) Then
MailMsg.To.Add(emailString)
End If
Next

MailMsg.From = New MailAddress(ConfigurationManager.AppSettings.Get("mailFrom"))
MailMsg.Subject = strSubject
MailMsg.Body = sbEmail.ToString
MailMsg.Body = FormatEmailBody() ??????
MailMsg.IsBodyHtml = True

MailObj.Host = ConfigurationManager.AppSettings.Get("SmtpServer")

MailObj.Send(MailMsg)
 
yes is defined by you.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top