Geekpedia Programming Tutorials






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

On Sunday, January 15th 2006 at 08:24 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 7 votes)
Advertisement
More ASP.NET Resources
This error normally occurs when you are trying to access the value of a session variable such as Session["UserID"] and you're not doing that from a webform, usercontrol or a class that inherits from System.Web.UI.

In this situation you can still access the session variable, but using a different path.
For example to access a session variable named UserID you would normally use Session["UserID"]; however, if the error The name 'Session' does not exist in the current context is returned, use the following path for retrieving the value of the session variable:

HttpContext.Current.Session["UserID"];

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by nishshanka on Tuesday, February 14th 2006 at 12:07 AM

Thanks lot. you saved my time..
it is working nice

by Jeff on Tuesday, May 16th 2006 at 09:51 PM

Amen, thank you, lifesaver...

by semigeek on Thursday, June 8th 2006 at 01:45 AM

Andrei u are the man!

by Blake on Friday, June 9th 2006 at 03:13 PM

Great post, this has helped a ton in transition from ASP to ASP.NET. In addition, HttpContext.Current has all of the web objects you may need from your classes like Request, Reponse, etc.

by KT on Thursday, July 6th 2006 at 11:02 AM

Thanx a lot was looking for it.

by Agustin on Wednesday, December 13th 2006 at 12:05 PM

Gorgeous POST!!!
Thanx

by Ed on Monday, December 18th 2006 at 07:21 PM

Oh! Thank you very much.

there is God guiding me to your site

by Fostec on Monday, January 1st 2007 at 05:57 AM

Thanks very much!

by nikhil on Monday, January 8th 2007 at 04:52 AM

Great post, i had to struggle a lot till i saw your post. Thanks a lot

by Jan on Monday, January 15th 2007 at 12:23 AM

Great this was what I needed

by Fay on Monday, February 19th 2007 at 05:03 AM

Thank you Andrei, you're a star.

by let me add to the co on Thursday, April 5th 2007 at 02:40 PM

THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!

by on Wednesday, April 18th 2007 at 10:25 AM

Great Help to me!

by pranay on Friday, May 18th 2007 at 04:19 AM

Great Help!!! Thanks lots

by on Tuesday, June 12th 2007 at 10:58 AM

Awesome Post, so easy yet so helpful!!

by Tdog Moneyman on Friday, June 22nd 2007 at 06:08 PM

This is better. I guess no one here has tried to access Session from a backing class? Don't suck the whole System.Web class into your new class though!!

System.Web.HttpContext.Current.Session["UserID"]

by Pat Markland on Thursday, August 30th 2007 at 11:16 PM

YES!!! Thank you!

by Remo on Friday, September 21st 2007 at 06:14 AM

Thanks a lot, it helps me a lot

by sKiLLer on Monday, October 8th 2007 at 11:52 AM

Excellent!

by bbbbb on Friday, November 2nd 2007 at 02:40 AM

Good!

by matt on Tuesday, November 13th 2007 at 06:48 AM

Modern Day Hero.

THANKS!

by Robot on Thursday, December 6th 2007 at 05:29 AM

FANKoooooooooooooo!

by xorit on Sunday, January 6th 2008 at 12:06 PM

That did it. Saved me lots of time and frustration.

by Poo on Thursday, March 13th 2008 at 05:28 AM

Great ,,, thnx so much

by J on Saturday, May 10th 2008 at 03:19 AM

tht didnt work...

by Abdalah on Friday, September 12th 2008 at 03:06 PM

Thank you Very much , it's work for me

by Gary on Tuesday, September 30th 2008 at 03:34 PM

There is a God. Alleluiah!

by Sanja on Monday, October 6th 2008 at 10:26 AM

Thanks

by Rabab Dairkee on Friday, January 16th 2009 at 03:00 PM

Thanks !!!
It worked

by Hades on Sunday, February 15th 2009 at 08:44 AM

It doesnot work 4 me :(

HttpContext.Current.Session["variable name"];

does not even exist!!!

by shc on Tuesday, March 3rd 2009 at 09:16 PM

This works well for me.


HttpContext.Current.Session["variable name"];

to HADES, have you added necessary IMPORT / USING ?
in this case, maybe:

using System.Web;

by Phan Cong Phuoc on Tuesday, March 31st 2009 at 12:26 AM

I have aldready imported system.Web.UI, but I cannot create Session["TitleID"]
but I can use your code: Http.Current.Session
It work, thank
But I wonder what is the diffrence
Could you tell more
Thank you

by Gabs on Monday, May 25th 2009 at 05:31 AM

Yep, That worked! Cheers!

by Bechay on Wednesday, July 29th 2009 at 09:59 PM

super thanks for this post!!

by Ram on Friday, August 14th 2009 at 07:59 AM

Really Super...Thanks

by James on Monday, August 24th 2009 at 10:33 AM

Nice!

I could not access my Session object in my class until I added "HttpContext.Current".

Before:
LoggedInUser user = (LoggedInUser)Session["LoggedInUser"];

After:
LoggedInUser user = (LoggedInUser)HttpContext.Current.Session["LoggedInUser"];

Thanks again!


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs ASP.NET Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons