'Session' does not exist in the class or namespace ....

G

Guest

I inherited some code from someone, and I am trying to use Session to store
some information that I need. For some reason, I can see that in another
file, the previous developer used Session["blahblah"] and assigned it to a
value, but when I try and do the same thing in a different part of the code,
(in a different namespace, if that helps), I get a compilation error, saying
that it doesn't exist.

I am using VS .NET 2003 and when I bring up the other file, I can mouse over
the Session object and see that it is a
System.Web.SessionState.HttpSessionState UserControl.Session object. But,
when I go to the file that I am trying to edit, when I mouse over it, it is
unrecognized. It thinks I'm trying to use a variable called Session, instead
of using the system one. Does anybody have any idea why this is happening?

Brian
 
G

Guest

The class / assembly will need to have a reference set to System.Web.
Then you can access the current Session object with:

HttpContext.Current.Session

Hope that helps.

Peter
 
H

Hans Kesting

I inherited some code from someone, and I am trying to use Session to store
some information that I need. For some reason, I can see that in another
file, the previous developer used Session["blahblah"] and assigned it to a
value, but when I try and do the same thing in a different part of the code,
(in a different namespace, if that helps), I get a compilation error, saying
that it doesn't exist.

I am using VS .NET 2003 and when I bring up the other file, I can mouse over
the Session object and see that it is a
System.Web.SessionState.HttpSessionState UserControl.Session object. But,
when I go to the file that I am trying to edit, when I mouse over it, it is
unrecognized. It thinks I'm trying to use a variable called Session, instead
of using the system one. Does anybody have any idea why this is happening?

Brian

The "Session" property that a usercontrol has, is not some magical
"system variable", but just a regular property of the (base class of
the) user control.

From within a different class, you can access the Session using
System.Web.HttpContext.Current.Session. Watch out: this will only work
if there *is* a "current httpcontext", that is, if the call to this
method is the result of some Request. If this method is called from a
timer event for instance, then HttpContext.Current is null.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top