Hi,
In my ASP.NET project, I have a session variable that is assigned on a login
page. In another .cs file (in another directory), I'm trying to use
if((string)Session["variable"] == "something")
When I compile, it tells me that Session does not exist in the calss or name
space.
My hypothesis is that it is because this directory is compiled before the di
rectory that actually has the Session variable's declaration. Any ideas on
how to fix this?
thanks.
George"Session" is not a global variable, it is a property of the
System.Web.UI.Page object.
When you use Session["variable"] inside an aspx page, it is actually
translating to this.Session["variable"].
When you are in another class which is not an aspx page derived off of the
Page class, it has no access to the Session object.
You either need to pass a reference to the Session object into your other
class (yuck), or pass the value itself as a parameter/property/etc.
Michael J. Mooney
MCP+SB, MCAD, MCSD
"George" <anonymous@.discussions.microsoft.com> wrote in message
news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> Hi,
> In my ASP.NET project, I have a session variable that is assigned on a
login page. In another .cs file (in another directory), I'm trying to use
> if((string)Session["variable"] == "something")
> When I compile, it tells me that Session does not exist in the calss or
namespace.
> My hypothesis is that it is because this directory is compiled before the
directory that actually has the Session variable's declaration. Any ideas
on how to fix this?
> thanks.
> George
Or you can grab the application object and then access the session:
using System.Web; //Need this at top
HttpApplication ha = HttpContext.Current.ApplicationInstance;
string mySessionValue = ha.Session["MySessionValue"];
Very simple:
VB.NET
Dim ha As HttpApplication = HttpContext.Current.ApplicationInstance
Dim mySessionValue As String = ha.Session("MySessionValue")
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
****************************************
********
Think Outside the Box!
****************************************
********
"Michael J. Mooney" <mike_mooney@._yahoo.com.NOSPAM> wrote in message
news:%23tyjDyLTEHA.2336@.TK2MSFTNGP10.phx.gbl...
> "Session" is not a global variable, it is a property of the
> System.Web.UI.Page object.
> When you use Session["variable"] inside an aspx page, it is actually
> translating to this.Session["variable"].
> When you are in another class which is not an aspx page derived off of the
> Page class, it has no access to the Session object.
> You either need to pass a reference to the Session object into your other
> class (yuck), or pass the value itself as a parameter/property/etc.
> --
> Michael J. Mooney
> MCP+SB, MCAD, MCSD
> "George" <anonymous@.discussions.microsoft.com> wrote in message
> news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> login page. In another .cs file (in another directory), I'm trying to use
> namespace.
the
> directory that actually has the Session variable's declaration. Any ideas
> on how to fix this?
>
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment