Session Variable

Discussion in 'ASP .Net' started by =?Utf-8?B?R2Vvcmdl?=, Jun 7, 2004.

  1. 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
    =?Utf-8?B?R2Vvcmdl?=, Jun 7, 2004
    #1
    1. Advertising

  2. "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" <> wrote in message
    news:...
    > 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
    Michael J. Mooney, Jun 7, 2004
    #2
    1. Advertising

  3. 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:%...
    > "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" <> wrote in message
    > news:...
    > > 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

    >
    >
    Cowboy \(Gregory A. Beamer\) [MVP], Jun 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smythe
    Replies:
    3
    Views:
    1,228
    Jeff Smythe
    Jan 2, 2004
  2. Replies:
    1
    Views:
    1,071
    Bruce Barker
    Nov 14, 2005
  3. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,254
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  4. chowchho
    Replies:
    7
    Views:
    618
    Patrice
    Mar 28, 2008
  5. Jazzis
    Replies:
    2
    Views:
    234
    Jazzis
    Sep 23, 2003
Loading...

Share This Page