Suppose the user login by entering his or her details
When clicking on Login button, I m setting the session variable

Session["UserName"] = txtUserName.Text;

I want dat when he or she
clicks on sign out(hyperlink)..I want to expire the session, can somebody tell me how to expire the session?

Or there is if any other way to do,what i want.plz tell me!

Recommended Answers

All 8 Replies

Session.Abandon();

Hi Use session.Abandon().
It will automatically closes all the sessions opened upto that function.

session.Abandon() 'or' session.clear() will do..

can u tell me wats the diff b/w the foll....ALL three does the same work or not!!!! When 2 use Session.Abandon();, when to use Session.Clear(); , When to use Session.RemoveAll()?


Session.Abandon();

Session.RemoveAll(); This removes all the sessions in the current Scope

Session.Clear();

Session.RemoveAll() and Session.Clear() are same. Both methods removes all keys and values from the session-state collection. But the current Session is not cancelled. That is Session.SessionID will be the same before and after calling these two methods.

But Session.Abandon() will cancel the current Session. That is, Once the Abandon method is called, the current session is no longer valid and a new session can be started.

Abandon causes the Session_OnEnd() event in the Global.asax to be raised. A new Session_OnStart() event will be raised on the next request.

Using these methods depends on your requirement. For example, you can call Session.Abandon() when the use logout your application.

Also you may want to clear all the keys in the Session object when the user goes to a landing page, for example, home page. In this scenario, you can use Session.Clear().

ramesh i'm very impressed but i had one query...

when session.abandon() is called and wot if my server don;t have

global.asax file....does it gonna work...just like u said..!

The Session.Abondon will cancel the session even if you don't have Global.asax file. The only thing is the Session_OnEnd() will not be executed.

The Session.Abondon will cancel the session even if you don't have Global.asax file. The only thing is the Session_OnEnd() will not be executed.

thxn bro..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.