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. Advertisements

  2. Green

    SevDer Guest

    Test it against null.
    And as in your example, when it is null, it will complain with the given
    error message.
     
    SevDer, Sep 10, 2004
    #2
    1. Advertisements

  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.

    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
     
    Kevin Spencer, Sep 10, 2004
    #4
  5. Green

    Joined:
    Sep 21, 2011
    Messages:
    2
    Likes Received:
    0
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.