What is difference between Session.Abandon() and Session.Clear()? [Resolved]

Posted by Kundan64 under ASP.NET on 1/16/2013 | Points: 10 | Views : 20998 | Status : [Member] | Replies : 6
What is difference between Session.Abandon() and Session.Clear()?




Responses

Posted by: harikrishna149-10896 on: 1/16/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Session.Abandon()

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.

When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.

For example, in the following script, the third line prints the value Hari. This is because the Session object is not destroyed until the server has finished processing the script.

<%
Session.Abandon
Session("MyName") = "Hari"
Reponse.Write(Session("MyName"))
%>

If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with the previous Session object when the page containing the previous example finished processing.
The server creates a new Session object ( with a new SessionID) when you open a subsequent Web page, after abandoning a session. You can store variables and objects in this new Session object.

Session.Clear()

The Clear method removes all keys and values from the current session. Compare to Abandon method, it doesn't create the new session, It just make all variables in session to NULL.

Thanks & Regards
Hari Krishna

Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 1/16/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi kundan ,

session.clear : It is used to clear all the keys in the session

session.abondon : It is used to clear the objects stored in the session


Please once go through this link it has a valuable solution
http://stackoverflow.com/questions/1470445/what-is-the-difference-between-session-abandon-and-session-clear

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 1/16/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
Session.Abandon() will end current session by firing Session_End and in the next
request, Session_Start will be fire.

Session.Clear( ) just clears the session data without killing it. With session.clear
variable is not removed from memory it just like giving value null to this session.

Session ID will remain same in both cases, as long as the browser is not closed.

Please Mark As Answer if this helps you

.

Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/21/2013 [Member] Starter | Points: 25

Up
0
Down
but wehn i am storing counter variable in session
after closing the browser
it again starts from where it left
why this is hapng?????

Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjitpawar49-24470 on: 11/6/2013 [Member] Starter | Points: 25

Up
0
Down
May be its come from browser cache memory...

Be happy always

Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi
1). We know The Abandon method is used to clear out session data and raise the Session_End event defined in the Global.asax and The clear method Clears all keys & values from the session-state item collection.

2). Session.Clear() clears the all session variables value whereas Session.Abandon() vanishes(kills) the session.

3). Session.Abandon() will end current session by firing Session_End and in the next
request, Session_Start will be fire whereas Session.Clear() just clears the session data without killing it. With session.clear
variable is not removed from memory it just like giving value null to this session.

Session ID will remain same in both cases, as long as the browser is not closed.

4). Session.Abandon(): This will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

Session.Clear(): Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.
The Clear method removes all keys and values from the current session. Compare to Abandon method, it doesn’t create the new session, It just make all variables in session to NULL.



Kundan64, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response