Session variables in global.asax

Posted on 2006-06-02
Last Modified: 2010-11-04
Currently we are setting up a customs error page in our site.  When an error occurs, the user is directed to the custom error page and an email is fired off from global.asax as to what occured with the compiler throug the following code:  

Protected Sub Application_Error(ByVal Sender as Object, ByVal e as EventArgs)
    Dim ErrorDescription As Exception = Server.GetLastError
    Dim url As String
    Dim body As String

    If Not (ErrorDescription Is Nothing) Then
        ErrorDescription = ErrorDescription.InnerException
        'Build My message body
            body = "<b>Error Message:</b><br>" & ErrorDescription.Message & _
                     "<br><br><b>Error Source:</b><br>" & ErrorDescription.Source & _
                    "<br><br><b>Error Type:</b><br>" & ErrorDescription.GetType.ToString() & _
                    "<br><br><b>Target Site:</b><br>" & ErrorDescription.TargetSite.ToString() & _
                    "<br><br><b>Stack:</b><br>" & ErrorDescription.StackTrace & _
        url = Request.Url.AbsoluteUri
        Dim mailMessage As New MailMessage
        mailMessage.From = ""
        mailMessage.To = ""
        mailMessage.Subject = "ATTENTION: Web Site Error"
        mailMessage.BodyFormat = MailFormat.Html
        mailMessage.Body = body
        mailMessage.Body += url
        System.Web.Mail.SmtpMail.SmtpServer = "SMTPServer"
    End If
End Sub

While the user is logged in the site has the username as a session variable.  How do I get the global.asax file to pass the username to me through this email?
I've added Session.Item("SSN").ToString and all that does is break and make it so it doesn't send an email at all.  Any ideas? Thanks in advance for your assistance.
Question by:NBAIS
    LVL 10

    Expert Comment

    LVL 4

    Expert Comment

    Try User.Identity
    LVL 4

    Expert Comment

    Try User.Identity.Name

    Author Comment

    How does User.Identity.Name reference the session variable that I need it to send me?
    LVL 4

    Expert Comment

    >> How do I get the global.asax file to pass the username to me through this email?

    User.Identity.Name will give the logged in username. Thats why I pointed out that.

    Author Comment

    System.Web.HttpContext.Current.Session.Item("SSN") breaks it so that it doesn't send an error and User.Identity.Name sends an empty string because there isn't really a user other than a variable stored in Session("SSN").
    LVL 4

    Accepted Solution

    How abt  sender.Context.Session("SSN")?

    Author Comment

    Any reference I make to the session kills the process.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now