locked
Killing / removing 2 sessions RRS feed

  • Question

  • User410642551 posted

    Hello,

    I am having some issues with killing 2 sessions I have in my application. It is in asp.NET.
    The application has 2 pages that takes data. Page 1 is session["ABC"] and data is taken from dropdownlist and textboxes. Page 2 is session["EFG"] and this page has a gridview that has dropdownlist and textboxes.

    In Page 1, I have a <CANCEL> button that when the user clicks, they are asked to cancel. Then they are direct to the <HOME> page and the session["ABC"] is removed as the code I have for it works fine.

                Session.RemoveAll();
                Session.Abandon();
                Response.Redirect("Home.aspx");




    In Page 2, I also have a <CANCEL> button and when the user clicks, they are again asked to cancel. They are direct to the <HOME> page. This is my issue. Session["ABC"] and Session["EFG"] does not get remove or abandon. When the user cancels from Page 1, sessions["ABC"] is killed, but when user cancels from page 2, session["EFG"] does not get killed.

    The data from both pages get saved temporarily and I think this may be the issue. How can I kill 2 sessions when the user clicks CANCEL?

    Any help or suggestion is appreciated!

    Wednesday, April 24, 2013 4:35 PM

Answers

  • User281315223 posted

    Typically - the Clear and Abandon methods should take care of any instances, but will likely need to be used in each location : 

    //Removes all Session variables
    Session.Clear();
    //Abandons the Session
    Session.Abandon();
    
    //Performs your Redirect
    Response.Redirect("YourPage.aspx");

    The Clear method will remove any and all Session variables / keys from the Session collection and the Abandon method will actually dispose of anything Session-related at the end of the current Request. (This is important as if you were just to call the Abandon method and attempt to access any Session values, they would still exist.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2013 5:21 PM

All replies

  • User281315223 posted

    Typically - the Clear and Abandon methods should take care of any instances, but will likely need to be used in each location : 

    //Removes all Session variables
    Session.Clear();
    //Abandons the Session
    Session.Abandon();
    
    //Performs your Redirect
    Response.Redirect("YourPage.aspx");

    The Clear method will remove any and all Session variables / keys from the Session collection and the Abandon method will actually dispose of anything Session-related at the end of the current Request. (This is important as if you were just to call the Abandon method and attempt to access any Session values, they would still exist.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2013 5:21 PM
  • User1196771204 posted

    Hi Rion,

    Thanks for sharing the knowledge about Sessions. Appreciate it! :) 

    Thursday, April 25, 2013 3:21 AM
  • User410642551 posted

    Thanks so much!

    Thursday, April 25, 2013 10:10 AM