Friday, November 12, 2010

The name 'Session' does not exist in the current context

Today while working on an ASP.NET web application I faced this problem accessing session variable in a C# file. After thinking for a while I got the problem. Problem was that I was trying to access Session variable from a class file in the library project, and this file was neither web form, user control or a class derived from System.Web.UI.

Session variables like Session["ProjectID"] can only be accessed from Web Forms, custom controls and from class inherited from System.Web.UI

After googling a bit I found that to access value of session variable in this case we can use line like below:

HttpContext.Current.Session["ProjectID"];


Similar sort of problem I also faced while trying to get root directory of a windows application. This time while development I tried code line "System.Windows.Forms.Application.StartupPath" in visual studio quickWatch and found it to be giving correct answer(Application root directory path) but as soon as I thought to use it in my project, it refused to build saying "namespace Windows can not be found, please check for missing assembly reference". Problem in this case was similar to above i.e, this line can't be accessed from a non windows form file.

About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!