I added a class in my project and when I type for session it is not coming in intelliscense. Is session can available in classes?
4 Answers
As far as I can understand, you're looking for
using System.Web;
// ...
var currentSession = HttpContext.Current.Session;
var myValue = currentSession["myKey"];

- 11,101
- 2
- 41
- 51
-
sorry mr.herzmeister httpcontext is not getting. i am saying in classes i think u get my point. i use the name sapce also even though it is not coming – Surya sasidhar Jan 11 '10 at 08:30
-
it is coming in aspx page i want in classes – Surya sasidhar Jan 11 '10 at 08:36
-
Do you mean that your class is not part of the website? Is it a library project? Then you have to add a reference to the System.Web assembly. Without it, the namespace with the same name is also there but only with very few types. – herzmeister Jan 11 '10 at 09:52
-
i am working on three tire architecture in that i took three projects in one solution explorer.in that one project is datalayer (it is a class) in that i not getting session – Surya sasidhar Jan 11 '10 at 12:26
-
In Visual Studio, first make sure that your class library project is not targeted to the Client Profile, but uses the full .net framework. Then right-click on your class library project and choose "Add Reference". After that, under the .NET tab choose the "System.Web" Assembly dll. You should then be able to access the session via HttpContext.Current. – herzmeister Jan 11 '10 at 13:51
Use this System.Web.HttpContext.Current.Session["Key"];

- 507
- 4
- 17

- 1,657
- 19
- 23
What you get from the collection is a reference to Object, you have to cast the reference to your specific class to get a reference that you can use to access the members of your class:
((MyClass)Session["TheKey"]). ...
If the problem is that you can't access the Session object at all, you need a reference to the current context. You get that by using the static property HttpContext.Current
:
HttpContext.Current.Session["TheKey"]
To get access to the HttpContext class, you need to tell the compiler where it is:
using System.Web;
You can also specify the complete name space directly without a using
directive:
System.Web.HttpContext.Current.Session["TheKey"]
You also need to add a reference to the System.Web.dll library in your project.

- 687,336
- 108
- 737
- 1,005
-
Mr. Guffa could u please tell me in details thank you for response. i am accessing from external class library – Surya sasidhar Jan 11 '10 at 08:44
In your class library project, add reference to System.Web
Then use Session
as in examples before.
It doesn't need to be an ASP.NET application, a class library will do.
I have seen this used even in WinForms applications because it still works.
-
ya Mr.Leon i add reference system.web i am getting only HttpContext but i am not getting "Current.Session" thank you for reference – Surya sasidhar Jan 12 '10 at 03:39