title: ASP.NET: Access Session variable in global.asax
body: <p>I have an ASP.NET application and in the Global.asax ' Application Error Event, I am calling a method to trace/log the error. I want to use the session variable content here. I used the below code </p>
  
  <pre><code>void Application_Error(object sender, EventArgs e)
  {
       //get reference to the source of the exception chain
       Exception ex = Server.GetLastError().GetBaseException();
  
       //log the details of the exception and page state to the
       //Windows 2000 Event Log
       GUI.MailClass objMail = new GUI.MailClass();
       string strError = "MESSAGE: " + ex.Message + "&lt;br&gt;&lt;br&gt;&lt;br&gt;" + "SOURCE: " + ex.Source + "&lt;br&gt;FORM: " + Request.Form.ToString() + "&lt;br&gt;QUERYSTRING: " +    Request.QueryString.ToString() + "&lt;br&gt;TARGETSITE: " + ex.TargetSite + "&lt;br&gt;STACKTRACE: " + ex.StackTrace;
       if (System.Web.HttpContext.Current.Session["trCustomerEmail"] != null)
       {
           strError = "Customer Email : " + Session["trCustomerEmail"].ToString() +"&lt;br /&gt;"+ strError;
       }
  
       //Call a method to send the error details as an Email
       objMail.sendMail("test@gmail.com", "myid@gmail.com", "Error in " + Request.Form.ToString(), strError, 2);   
  } 
  </code></pre>
  
  <p>I am getting an error in the line of code where I am accessing the session variable. Visual Studio is telling that </p>
  
  <blockquote>
    <p>Session is not available in this context</p>
  </blockquote>
  
  <p>How can I get rid of this?</p>