Session state is not available in this context - Only When Debugging

Posted on 2008-11-12
Last Modified: 2013-12-17

I have an Application_Error handler that collates various information. Part of this information includes Session variables e.g. string Username = "Username: " + Session["UserName"];

This all works fine except if I want to trace program code in the VS2008 debugger. I then get the error "Session state is not available in this context" when it reaches this line.

Thank you,

Question by:tedporter
    LVL 4

    Expert Comment

    by:Tristan Shortland
    Try using HttpContext.Current.Session("Value") to ensure you have the current context...

    Author Comment


    Thanks for the suggestion. I have tried it and now get a new error message

    System.NullReferenceException was unhandled by user code

    As previously mentioned, the code works fine in Production just not when debugging.

    Author Comment

    Hi All,

    This might help - The original error also contained the following:

    System.Web.HttpException was unhandled by user code
      Message="Session state is not available in this context."
           at System.Web.HttpApplication.get_Session()
           at ASP.global_asax.Application_Error(Object sender, EventArgs e) in d:\Documents and Settings\EPorter\My Documents\My Code\Visual Studio 2008\WebSites\eNjoin\Global.asax:line 41
           at System.EventHandler.Invoke(Object sender, EventArgs e)
           at System.Web.HttpApplication.RaiseOnError()
    LVL 4

    Expert Comment

    by:Tristan Shortland
    Are you debugging a class library stand-alone? You need to launch it from a test web app if you are, so that the web context is available...

    Accepted Solution


    I fixed my problem. It was to do with my web menu (Coalesys Mecury UI) control, using a hidden menu that was referencing some GIF images that were not present.

    Not sure why this ony raised it head in debug mode and not in release but I took the references out and it now debugs fine.

    I will let the guys over at Coaleysy know.

    Thanks for you help.

    Expert Comment

    Thanks for explanation

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction (All good things must come to an end ( The original MySQL API ( has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    754 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

    24 Experts available now in Live!

    Get 1:1 Help Now