Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,
I want to clear my session variable on click of signout button.Here I am giving my code,Please check it and resolve.Thanks in advance

This is my login form code
private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            sConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
            Label2.Visible = false;

        }


this is my submit button code of login form
private void cmdsub_Click(object sender, System.EventArgs e)
		{
			sqlTest = "select * from tbl_user where du_user='" + txtuser.Text+"' and du_pass='" + txtpass.Text +"'";
			OleDbConnection dbConn = new OleDbConnection(sConnectionString);
			dbConn.Open();
			OleDbDataAdapter sqlad=new OleDbDataAdapter(sqlTest,dbConn);
			DataSet ds=new DataSet();
			sqlad.Fill(ds,"tbl_user");
			int rowcount=ds.Tables["tbl_user"].Rows.Count;
			if(rowcount>0)
			{
                Session["loginid"] = txtuser.Text;
                Response.Redirect("ListDetail.aspx");
                Label2.Visible = false;
			}
			else
			{
                Label2.Visible = true;
                Label2.Text = "";
                Label2.Text="Your id and password do not match";
			}
			
		}

this is my suggestion form code where i have to go sfter clicking on submit button of login form.code is below
mean this is page load of suggestion form
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
           HttpContext.Current.Response.Cache.SetNoServerCaching();
           HttpContext.Current.Response.Cache.SetNoStore();

if (Page.IsPostBack == false)
                {
                    sConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                    ddlministry.Items.Clear();
                    sqlTest = "select distinct(du_suggestname) from tbl_suggestion";
                    dbConn = new OleDbConnection(sConnectionString);
                    dbConn.Open();
                    OleDbDataAdapter sqlad = new OleDbDataAdapter(sqlTest, dbConn);
                    DataSet ds = new DataSet();
                    sqlad.Fill(ds, "tbl_suggestion");
                    for (int i = 0; i <= ds.Tables["tbl_suggestion"].Rows.Count - 1; i++)
                    {
                        ddlministry.Items.Add(ds.Tables["tbl_suggestion"].Rows[i][0].ToString());
                    }
                }


This is my submit button code of suggestion.from where i choose my data from dropdownlist name ddlministry and click on submit button.
below code is for submit button of suggestion form

sConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                sqlTest = "select * from tbl_suggestion where du_suggestname='" + ddlministry.Text + "'";
               OleDbConnection dbConn1 = new OleDbConnection(sConnectionString);
               dbConn1.Open();
               OleDbDataAdapter sqlad1 = new OleDbDataAdapter(sqlTest, dbConn1);
               DataSet ds1 = new DataSet();
               sqlad1.Fill(ds1, "tbl_suggestion");
               DataView source = new DataView(ds1.Tables["tbl_suggestion"]);
               DataGrid1.DataSource = ds1;
               DataGrid1.DataBind();



A button is linkbutton from where i logeed out and code is below
Session["loginid"] = null;
Response.Redirect("login.aspx");

Problem is that when i click on logout button then it come into login page again but if i click on back button of internate explorer then it again come in suggestion form with data.but i want that after logout i don't want go into suggestion form pressing on back button also
Please help me thanks in advance
Posted
Updated 15-May-11 18:32pm
v2

you can use :-
session.TimeOut=5;
session.Remove("username");
session.RemoveAll();
session.Clear();
if(session["username"]==null)
{
Response.Redirect("Default.aspx");
}
 
Share this answer
 
Session.Clear();
Session.Timeout = 1;
Session.RemoveAll();
Session.Abandon();
this code is working
 
Share this answer
 
Session.Clear();
            Session.Timeout = 1;
            Session.RemoveAll();
            Session.Abandon();


and navigate to login page
Response.Redirect("Login.aspx");



And it is good habit to navigate to a sign out page 1st and on the page load have the above code..
hope this will help
 
Share this answer
 
Comments
singh7pankaj 16-May-11 1:40am    
thanks It working.Thank u very much
anvas kuttan 16-May-11 2:13am    
welcome happy coding
It's not session that is creating this. It's Browsers cache.

have a look at this tip: Browser back button issue after logout[^]
 
Share this answer
 
Use this code on your logout link

Session.Abondon();
Response.Cookies.Clear();
FormsAuthentication.SignOut();
Response.Redirect("~/Login.aspx");


Then put this on your Page_Load event for Login.aspx

Response.Cache.SetNoStore();


Happy coding!
 
Share this answer
 
Comments
singh7pankaj 16-May-11 1:29am    
FormsAuthentication keyword not shown in c# list.It gives error
the name FormsAuthentication does not exist in current context
Pong D. Panda 16-May-11 2:03am    
Remove that line of code if your not using form authentication. The fix for the back button is the code on Page_Load I indicated.
Try using session.clear() or session.abandon().
 
Share this answer
 
Comments
singh7pankaj 16-May-11 0:46am    
I used both one but it didn't work
senguptaamlan 16-May-11 1:19am    
in what respect you are saying that both are not working ? how you have tested that the session.clear() or session.abandon() is not working??
singh7pankaj 16-May-11 1:25am    
first i used session.abandon(); in click of signout and also session.clear but it didn't work
arunnarwal 16-May-11 4:18am    
session.Remove("sessionname");
or you can also use
session.RemoveAll();

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