Click here to Skip to main content
11,705,451 members (51,765 online)
Click here to Skip to main content

Tagged as

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

, 13 Apr 2011 CPOL 25.7K 7
Rate this:
Please Sign up or sign in to vote.
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#

License

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

Share

About the Author

Toniyo Jackson
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGetting an error Pin
Jesse Fatherree16-Oct-12 11:02
memberJesse Fatherree16-Oct-12 11:02 
I've seen lots of pages suggesting the same method but when I try this out, I get "Object reference not set to an instance of an object."

This code is in a .vb class in the /app_code folder. Status is a string.
HttpContext.Current.Session("Status") = Status

Any suggestions?
AnswerRe: Getting an error Pin
jaipoint13-Dec-13 20:21
memberjaipoint13-Dec-13 20:21 
AnswerRe: Getting an error Pin
meeram3925-Dec-13 5:54
membermeeram3925-Dec-13 5:54 
GeneralReason for my vote of 5 Good for beginners. Pin
Kunal_Chowdhury13-Apr-11 1:44
mvpKunal_Chowdhury13-Apr-11 1:44 
GeneralRe: Thanks Kunal Pin
Toniyo Jackson13-Apr-11 20:01
memberToniyo Jackson13-Apr-11 20:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 13 Apr 2011
Article Copyright 2011 by Toniyo Jackson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid