Session Variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George

    Session Variable

    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)Sess ion["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

    #2
    Re: Session Variable

    "Session" is not a global variable, it is a property of the
    System.Web.UI.P age 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@disc ussions.microso ft.com> wrote in message
    news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@mi crosoft.com...[color=blue]
    > Hi,
    >
    > In my ASP.NET project, I have a session variable that is assigned on a[/color]
    login page. In another .cs file (in another directory), I'm trying to use[color=blue]
    > if((string)Sess ion["variable"] == "something" )
    >
    > When I compile, it tells me that Session does not exist in the calss or[/color]
    namespace.[color=blue]
    >
    > My hypothesis is that it is because this directory is compiled before the[/color]
    directory that actually has the Session variable's declaration. Any ideas
    on how to fix this?[color=blue]
    >
    > thanks.
    >
    > George[/color]


    Comment

    • Cowboy \(Gregory A. Beamer\) [MVP]

      #3
      Re: Session Variable

      Or you can grab the application object and then access the session:

      using System.Web; //Need this at top

      HttpApplication ha = HttpContext.Cur rent.Applicatio nInstance;

      string mySessionValue = ha.Session["MySessionValue "];

      Very simple:

      VB.NET

      Dim ha As HttpApplication = HttpContext.Cur rent.Applicatio nInstance
      Dim mySessionValue As String = ha.Session("MyS essionValue")

      --
      Gregory A. Beamer
      MVP; MCP: +I, SE, SD, DBA

      *************** *************** *************** ***
      Think Outside the Box!
      *************** *************** *************** ***
      "Michael J. Mooney" <mike_mooney@_y ahoo.com.NOSPAM > wrote in message
      news:%23tyjDyLT EHA.2336@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > "Session" is not a global variable, it is a property of the
      > System.Web.UI.P age 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@disc ussions.microso ft.com> wrote in message
      > news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@mi crosoft.com...[color=green]
      > > Hi,
      > >
      > > In my ASP.NET project, I have a session variable that is assigned on a[/color]
      > login page. In another .cs file (in another directory), I'm trying to use[color=green]
      > > if((string)Sess ion["variable"] == "something" )
      > >
      > > When I compile, it tells me that Session does not exist in the calss or[/color]
      > namespace.[color=green]
      > >
      > > My hypothesis is that it is because this directory is compiled before[/color][/color]
      the[color=blue]
      > directory that actually has the Session variable's declaration. Any ideas
      > on how to fix this?[color=green]
      > >
      > > thanks.
      > >
      > > George[/color]
      >
      >[/color]


      Comment

      Working...