Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I face a strange problem, I use some session variable and try to destroy those using the code
C#
Session.Contents.RemoveAll()

It was work fine but suddenly it stop working, that is It cannot destroy my session variable. I don't know what's my fault. please help my.
Posted
Comments
Joezer BH 28-Aug-13 6:58am    
What happens?
Provide some more information

Sazzad said:
that is It cannot destroy my session variable

I am not sure about the nature of your problem, as you provided too few details, however,
you stated that you want to destroy the session, in which case I would suggest that you use
C#
Session.Abandon();


Note: When this method is called, the current Session object is not deleted until all of the script on the current page have been processed. This means that it is possible to access session variables on the same page as the call to Abandon, but not from another Web page.

Cheers,
Edo
 
Share this answer
 
v2
Comments
Herbisaurus 28-Aug-13 7:03am    
5!
Joezer BH 28-Aug-13 7:04am    
Thank you Herb!
sazzad37 28-Aug-13 7:10am    
example: a session variable
Session["gainMark"] = 0;
stored into a control page named control.aspx.cs . Say, it currently store 5. I want to destroy it when press a button. I use every code that you people provide but it still show 5 in result.
session.Clear();
session.Abdomen();
 
Share this answer
 
Comments
Joezer BH 28-Aug-13 7:36am    
session.Abdomen(); => Session.Abandon();
FTFY ;)
amolnarkhede 28-Aug-13 7:56am    
ow... :D ;)
Try using Session.Clear() or Session.Abandon(); The difference between the two is

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.

Regards..:)
 
Share this answer
 
Comments
Herbisaurus 28-Aug-13 7:03am    
5!
Thanks7872 28-Aug-13 7:06am    
Thank you :-)
C#
If you want to remove a specific item from the session use:

Session.Remove("YourItem");
 or else you can use
Session.Abandon()
 
Share this answer
 
v2
try below:

C#
Session.Remove("caption");
 
Share this answer
 
try like this
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack )
       Session ["name"]="Naveen";
   }
   protected void btnAdd_Click(object sender, EventArgs e)
   {
       if(Session["name"].ToString ()!="")
       {

           Session["name"].ToString();
       Session.Clear();
       }

   }


i have tested this working fine
 
Share this answer
 
VB
in c#.net

Session.Remove("caption");

in vb.net

Session.Remove("caption")
 
Share this answer
 
use this method Session.Clear();
 
Share this answer
 
Comments
sazzad37 28-Aug-13 6:59am    
Not working.
SQL
To destroy the session variable u can use remove() method

in c#.net

Session.Remove("SessionName");

in vb.net

Session.Remove("SessionName")

Hope cleared ur doubt
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900