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


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,

tedporterIT Technical Services ConsultantAsked:
Who is Participating?
tedporterConnect With a Mentor IT Technical Services ConsultantAuthor Commented:

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.
Tristan ShortlandCommented:
Try using HttpContext.Current.Session("Value") to ensure you have the current context...
tedporterIT Technical Services ConsultantAuthor Commented:

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.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

tedporterIT Technical Services ConsultantAuthor Commented:
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()
Tristan ShortlandCommented:
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...
Thanks for explanation
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.