How to determine if a Session Variable exists?

Discussion in 'ASP .Net' started by Shapper, Jun 8, 2005.

  1. Shapper

    Shapper Guest

    Hello,

    How to determine if a Session Variable exists?

    In Page_Load I need to check is Session("MyVar") Exists.
    If it doesn't exist then I created it and give it a value:
    Session("myVar") = "Value"

    Thanks,
    Miguel
     
    Shapper, Jun 8, 2005
    #1
    1. Advertising

  2. Shapper

    Ziemowit Guest

    If variable doesn't exist then its null. Simlpy.

    Ziemowit
     
    Ziemowit, Jun 8, 2005
    #2
    1. Advertising

  3. Shapper

    Nawaz Ijaz Guest

    Simply do this..

    if ( Session("MyVar") == "" )
    {
    Session("MyVar") = "Your Value"
    }
     
    Nawaz Ijaz, Jun 8, 2005
    #3
  4. Ancient Zen problem:

    If a Session variable is set, and there is no class to read it, does it
    consume resources?

    --
    ;-)

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    news:...
    > Hello,
    >
    > How to determine if a Session Variable exists?
    >
    > In Page_Load I need to check is Session("MyVar") Exists.
    > If it doesn't exist then I created it and give it a value:
    > Session("myVar") = "Value"
    >
    > Thanks,
    > Miguel
    >
     
    Kevin Spencer, Jun 8, 2005
    #4
  5. Shapper

    Joined:
    Sep 21, 2011
    Messages:
    2
    session variable exists or not?

    Its difficult to check whether a session name exists or not..
    Because we cannot use a session instance name before it is being declared, means assigning the value to it. So it is better to use the following code. It is very bad programming. but if you want you can use it.

    try
    {
    string data = Session["sessionIds"].tostring();
    }
    catch(Exception ex)
    {
    // Do your handling code. ie, if you want to set the session value set here.
    }


    the idea behind is that using the session variable before declaring the value will thow an error. By this we can make sure that the session variable is not existing.
    If you want more specific just check what error throwing in this scenario and check whether the error is same as you required error and do the handling code.
    make sure to check whether the error is not coming from any other reasons.
    Usually the error will be "object reference not set to an instance of the object".
    Hope this will help some one..
     
    , Sep 21, 2011
    #5
    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. =?Utf-8?B?T3BpZQ==?=

    Determine if record exists

    =?Utf-8?B?T3BpZQ==?=, Feb 12, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    8,222
    Joe Fallon
    Feb 13, 2005
  2. Adam J Knight

    Test if Session Variable Exists!!

    Adam J Knight, Feb 17, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    11,077
    Ravi Ambros Wallau
    Feb 17, 2006
  3. Totan
    Replies:
    0
    Views:
    1,082
    Totan
    Apr 17, 2006
  4. OvErboRed
    Replies:
    2
    Views:
    624
    FeU Hagen
    May 22, 2004
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,309
Loading...

Share This Page