Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to clear session varibale value in logout event. but it won't works.
code is here
if (Page.IsPostBack == false)
            {
                string s = Session["loginid"].ToString();

                if (Session["loginid";].ToString() == "")
                {
                    Response.Redirect("errorpage1.aspx");
                }
            }
Posted
Updated 12-May-11 23:53pm
v2

Usually we use Session.Abondon() method to destroy all objects stored in session object if a user logs out.
If you need to clear only a particular session object, set it to null.
C#
Session["loginid"] = null;

And do a null check wherever you need to check if it has any value or not.
C#
if (Session["loginid"] == null)
{
    //redirect
}
else
{
    //user is logged in
}


Session["loginid"] = ""; still refers to a memory location, but if you assign the session object a null value, it will no more refer to any memory location, which in your case makes more sense.

BTW I also notice a syntax error in your code, if (Session["loginid";].ToString() == "")
The semi colon shouldn't be there.

Hope this helps!
 
Share this answer
 
Comments
raju melveetilpurayil 13-May-11 6:26am    
Good one :P my 5
singh7pankaj 13-May-11 6:47am    
It won't works.
I corrected it all but also its not done
Ankur\m/ 13-May-11 6:52am    
Did you try what I said? Moreover why the code is inside "Page.IsPostBack == false". Remove that as well.
singh7pankaj 13-May-11 6:58am    
Yes i remove all that but also it won't work
Ankur\m/ 13-May-11 7:09am    
Did you assign Session["loginid"] = null; after the user logs out?
u can do like

C++
Session["loginid"]="";
 
Share this answer
 
Comments
singh7pankaj 13-May-11 6:49am    
It won't works.I did that also.
Nick Reshetinsky 13-May-11 7:30am    
Session["loginid"] = null;
it's right cause if you assign "" (empty string) it would still reference some memory block

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