Click here to Skip to main content
14,357,676 members

Accessing session from a class file in Asp.Net [For Beginners]

Rate this:
4.78 (4 votes)
Please Sign up or sign in to vote.
4.78 (4 votes)
13 Apr 2011CPOL
For Beginners
The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because, ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, class file will not inherit System.web namespace. So we need access like below,
HttpContext.Current.Session("Session_Name")     // VB

HttpContext.Current.Session["Session_Name"];    // C#


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Toniyo Jackson
Software Developer
India India
No Biography provided

Comments and Discussions

QuestionGetting an error Pin
Jesse Fatherree16-Oct-12 12:02
memberJesse Fatherree16-Oct-12 12:02 
AnswerRe: Getting an error Pin
jaipoint13-Dec-13 21:21
memberjaipoint13-Dec-13 21:21 
AnswerRe: Getting an error Pin
meeram3925-Dec-13 6:54
professionalmeeram3925-Dec-13 6:54 
Check first whether it is null and then assign, like below:
For get:
string status = HttpContext.Current.Session("Status") == null ? Convert.ToString(HttpContext.Current.Session("Status") : string.Empty

For set:
if (Status != "")
  HttpContext.Current.Session("Status") = Status;

GeneralReason for my vote of 5 Good for beginners. Pin
Kunal Chowdhury «IN»13-Apr-11 2:44
mentorKunal Chowdhury «IN»13-Apr-11 2:44 
GeneralRe: Thanks Kunal Pin
Toniyo Jackson13-Apr-11 21:01
memberToniyo Jackson13-Apr-11 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 13 Apr 2011

Tagged as


7 bookmarked