test session exist

Discussion in 'ASP .Net' started by Green, Sep 10, 2004.

  1. Green

    Green Guest

    Hi,
    I want to test whether a sesion exist, what can i do?
    I use Session["name"].toString().length == 0 , but it will prompt the
    error saying object is not referenced. How can i test Session["name"]
    exist?

    Thanks in advance!
     
    Green, Sep 10, 2004
    #1
    1. Advertising

  2. Green

    SevDer Guest

    Green wrote:

    > Hi,
    > I want to test whether a sesion exist, what can i do?
    > I use Session["name"].toString().length == 0 , but it will prompt
    > the error saying object is not referenced. How can i test
    > Session["name"] exist?
    >
    > Thanks in advance!


    Test it against null.
    And as in your example, when it is null, it will complain with the given
    error message.

    --

    SevDer
    http://www.sevder.com
     
    SevDer, Sep 10, 2004
    #2
    1. Advertising

  3. Green

    Shiva Guest

    First, check for the existance of the item in the session state collection
    before accessing its properties:

    if (Session["name"] == null)
    {
    // name does not exist in the Session state collection.
    }
    else
    {
    // Access Session["name"] here...
    }

    Hope I got the question correctly.

    "Green" <> wrote in message news:eek:psd4iwaxppk4u20@dev-02...
    Hi,
    I want to test whether a sesion exist, what can i do?
    I use Session["name"].toString().length == 0 , but it will prompt the
    error saying object is not referenced. How can i test Session["name"]
    exist?

    Thanks in advance!
     
    Shiva, Sep 10, 2004
    #3
  4. And BTW, on the server side, the Session ALWAYS exists - unless it takes
    longer than 20 minutes for the Page to process. Just want to make sure
    there's no confusion between the Session Collection and its members.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    I get paid good money to
    solve puzzles for a living

    "Shiva" <> wrote in message
    news:...
    > First, check for the existance of the item in the session state collection
    > before accessing its properties:
    >
    > if (Session["name"] == null)
    > {
    > // name does not exist in the Session state collection.
    > }
    > else
    > {
    > // Access Session["name"] here...
    > }
    >
    > Hope I got the question correctly.
    >
    > "Green" <> wrote in message

    news:eek:psd4iwaxppk4u20@dev-02...
    > Hi,
    > I want to test whether a sesion exist, what can i do?
    > I use Session["name"].toString().length == 0 , but it will prompt the
    > error saying object is not referenced. How can i test Session["name"]
    > exist?
    >
    > Thanks in advance!
    >
    >
     
    Kevin Spencer, Sep 10, 2004
    #4
  5. Green

    Joined:
    Sep 21, 2011
    Messages:
    2
    check whether session name exists

    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. jova
    Replies:
    46
    Views:
    27,810
    Roedy Green
    Apr 18, 2006
  2. Y.S.
    Replies:
    3
    Views:
    1,025
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,101
    Phlip
    Jul 25, 2004
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    707
    Skybuck Flying
    Oct 4, 2009
  5. Markus Mohr
    Replies:
    7
    Views:
    251
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page