Session state is not available in this context in Global.asax

11,487

Solution 1

Global.asax file is generally used to track the application level events and those are shareable between the different logged in users so it might not be possible to get the session value in global.asax events.

For error logging you can use the httpModule

Below link for error logging using httpModule

http://www.codeproject.com/Articles/16171/An-HTTP-Module-for-ASP-NET-Error-Handling

Below link is for getting session values in httpModule

Can I access session state from an HTTPModule?

Solution 2

The application_error catches ALL exception in the application. Your error can originate from a point where there is no session.

(Quick example: from an async operation, or custom created thread) Best test : test for HttpContext.current != null and HttpContext.current.session != null before trying anything with session.

And using session is bad anyway 

Oh for you last point : If you put a breaking point anyway else than the “catch” in application error, your faulty thred/async could complete operation before visual studio stops the process. So in this case you CAN have a session, while in normal run (without breakpoint) you’ll get an exception

Share:
11,487
Amin Jariwala
Author by

Amin Jariwala

Updated on June 07, 2022

Comments

  • Amin Jariwala
    Amin Jariwala almost 2 years

    I am getting and error message when i'm trying to get Session value in Global.aspx:

    enter image description here

    I have tried another method to get Session value and result is same but message is different:

    enter image description here

    This error occur in Application_Error method and Session is not null but it shows that it is null. I have put this Session code in (HttpContext.Current.Session != null) condition so it is working fine but i want session value which is not getting with this method.

    enter image description here

    Please help me. Thanks