Answered by:
Destroy Session Variables

Question
-
User-797751191 posted
Hi
I have below code . I want to clear all Session Values. Is the below code correct.
Secondly how we can check that all Session variables are destroyed.
try { if (Session["InvoiceNo"] != null) { lblName.Text = Session["CustomerName"].ToString(); lblAddress.Text = Session["CustomerAddress"].ToString(); lblInvoiceNo.Text = Session["InvoiceNo"].ToString(); } } catch (Exception ex) { } finally { Session.Abandon(); }
Thanks
Monday, June 24, 2019 7:50 AM
Answers
-
User753101303 posted
Hi,
Use Session.Clear(); before Session.Abandon();
Not directly related but I never use an empty catch block (you want to fix programming errors rather than to just ignore them).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 8:04 AM -
User-1038772411 posted
Hello jssivalik,
According to your requirements you have to use .
The
Abandon
method should work (MSDN):Session.Abandon();
If you want to remove a specific item from the session use (MSDN):
Session.Remove("YourItem");
EDIT: If you just want to clear a value you can do:
Session["YourItem"] = null;
If you want to clear all keys do:
Session.Clear();
And how it works when session is destroyed/terminated
I hope this will help you
Thank you
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 8:33 AM
All replies
-
User753101303 posted
Hi,
Use Session.Clear(); before Session.Abandon();
Not directly related but I never use an empty catch block (you want to fix programming errors rather than to just ignore them).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 8:04 AM -
User-1038772411 posted
Hello jssivalik,
According to your requirements you have to use .
The
Abandon
method should work (MSDN):Session.Abandon();
If you want to remove a specific item from the session use (MSDN):
Session.Remove("YourItem");
EDIT: If you just want to clear a value you can do:
Session["YourItem"] = null;
If you want to clear all keys do:
Session.Clear();
And how it works when session is destroyed/terminated
I hope this will help you
Thank you
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 8:33 AM