I have created a class file in the App_Code folder in my application. I have a session variable
Session["loginId"]
I want to access this session variables in my class, but when I am writing the following line then it gives error
Session["loginId"]
Can anyone tell me how to access session variables within a class which is created in app_code folder in ASP.NET 2.0 (C#)
The answers presented before mine provide apt solutions to the problem, however, I feel that it is important to understand why this error results:
The
Session
property of thePage
returns an instance of typeHttpSessionState
relative to that particular request.Page.Session
is actually equivalent to callingPage.Context.Session
.MSDN explains how this is possible:
However, When you try to access this property within a class in App_Code, the property will not be available to you unless your class derives from the Page Class.
My solution to this oft-encountered scenario is that I never pass page objects to classes. I would rather extract the required objects from the page Session and pass them to the Class in the form of a name-value collection / Array / List, depending on the case.
Access the Session via the threads HttpContext:-
I had the same error, because I was trying to manipulate session variables inside a custom Session class.
I had to pass the current context (system.web.httpcontext.current) into the class, and then everything worked out fine.
MA
The problem with the solution suggested is that it can break some performance features built into the SessionState if you are using an out-of-process session storage. (either "State Server Mode" or "SQL Server Mode"). In oop modes the session data needs to be serialized at the end of the page request and deserialized at the beginning of the page request, which can be costly. To improve the performance the SessionState attempts to only deserialize what is needed by only deserialize variable when it is accessed the first time, and it only re-serializes and replaces variable which were changed. If you have alot of session variable and shove them all into one class essentially everything in your session will be deserialized on every page request that uses session and everything will need to be serialized again even if only 1 property changed becuase the class changed. Just something to consider if your using alot of session and an oop mode.
(Updated for completeness)
You can access session variables from any page or control using
Session["loginId"]
and from any class (e.g. from inside a class library), usingSystem.Web.HttpContext.Current.Session["loginId"].
But please read on for my original answer...
I always use a wrapper class around the ASP.NET session to simplify access to session variables:
This class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class, e.g like this:
This approach has several advantages:
This should be more efficient both for the application and also for the developer.
Add the following class to your web project:
Here is the implementation: