Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I have 4 Sessions username,finyear,company,branch

when select company form is loading, I want to clear the all the sessions except username. How to do it?

I am using following in page_load for select company form, but the errror display
'object reference is not set to an instance of an object'

Session.Remove("name")
Session.Contents.Remove("name")
Session.Abondon()
Session.Clear()


Please anybody help me

Thank you
Posted

like this:

C#
foreach (string key in Session.Keys)
{
    if("username" != Session[key])
    {
        Session.Remove(Session[key]);
    }
}
 
Share this answer
 
Comments
Rahul Rajat Singh 10-Jul-12 4:28am    
good answer. +5.
Manas Bhardwaj 10-Jul-12 4:35am    
thx!
devausha 10-Jul-12 5:49am    
I try this, But the Error displays 'Session has some invalid arguments'
pradiprenushe 10-Jul-12 6:29am    
which line actually getting this error? I think this will solve
change this in above code
if("username" != Convert.Tostring( Session[key]))
C#
Session["finyear"] = null;
Session["company"] = null;
Session["branch"] = null;
 
Share this answer
 
v2
Quote:
you can use
Session["finyear"] = null;
Session["company"] = null;
Session["branch"] = null;
 
Share this answer
 
Comments
devausha 10-Jul-12 5:47am    
I have already try this. But the Error is displayed "Object Reference is not set to an instance of an object"

Where we can give this code? within postback
if you use master page then write this in page load

C#
protected void Page_Load(object sender, EventArgs e)
   {
       Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       Response.Cache.SetNoStore();
       if (Session["SessionId"] == null)
       {
           Response.Redirect("login.aspx");
       }


   }

///
send some SessionId throw login.aspx
 
Share this answer
 

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