locked
new session after session.abandon RRS feed

  • Question

  • User-1163261216 posted

    hi

    if in my page_load i have session.abandon and right after that line im add Session["IsAbandon"] = true;

    when i click on button would i see the session["IsAbandon"] or it will be null?

    Thursday, December 22, 2011 2:13 AM

Answers

  • User-1971614856 posted

    This is because you are calling Session.Abandon() out side of if (!IsPostBack)  statement.

    Use following code in Page_Load and your issue will be solved.

    if (!IsPostBack)
    
    {
    
    Session.Abandon();
    
    }
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 22, 2011 2:39 AM

All replies

  • User-448512826 posted

    Hi,

    Check this article which explains the process on session.abandon

    http://support.microsoft.com/kb/899918

    Taken from above link -

    "When you abandon a session, the session ID cookie is not removed from the browser of the user. Therefore, as soon as the session has been abandoned, any new requests to the same application will use the same session ID but will have a new session state instance"

    Clear - Removes all keys and values from the session-state collection.

    Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out.
    It also raises events like Session_End.

    Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more likethrowing away the whole shelf.

    You say:

    when i Test Session Id Dont makes any change when i Abandone session

    This is correct while you are doing it within one request only.
    On the next request the session will be different. But the session ID can be reused so that the id will remain the same.

    If you will use Session.Clear you will have the same session in many requests.

    Generally, in most cases you need to use Session.Clear.
    You can use Session.Abandon if you are sure the user is going to leave your site.

    So back to the differences:

    1. Abandon raises Session_End request.
    2. Clear removes items immidiately, Abandon does not.
    3. Abandon releases the SessionState object and its items so it can ba garbage collected to free the resources. Clear keeps SessionState and resources associated with it.
    Thursday, December 22, 2011 2:23 AM
  • User-1971614856 posted

    You will get the Session["IsAbandon"] = true in button click because this will start a new session.

    Thursday, December 22, 2011 2:24 AM
  • User-279791546 posted

    Hi Roy.

    I think if you try to check whether a session is not present anymore then Session["IsAbandon"] is the incorrect way. By using this Session["IsAbandon"] you will create another session key-value pair.

    And yes, you will find the value of Session["IsAbandon"] at button click on the same page which will be "True".

    To check the session is valid or not, you can do something like this after Session.Abandon(): 

    if (Session.Keys.Count == 0)

    Hope this is what you are seeking. Or else if you want to do something else, you are free to reply :-)

    Thursday, December 22, 2011 2:26 AM
  • User-1163261216 posted

    1.i got the new session = null.

    2.my example is not what im tring to get, i just need to know if is possiable.

     but when i test it i get the new session after im doing postback as null.

    Thursday, December 22, 2011 2:36 AM
  • User-1971614856 posted

    This is because you are calling Session.Abandon() out side of if (!IsPostBack)  statement.

    Use following code in Page_Load and your issue will be solved.

    if (!IsPostBack)
    
    {
    
    Session.Abandon();
    
    }
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 22, 2011 2:39 AM