protected void BtnLogin_Click(object sender, EventArgs e)
{
String conn = "<Your connectionSTring>";
SqlConnection connection = new SqlConnection(conn);
connection.Open();
string q = "select Count(*) from users where username='" + txtuname.Text + "' AND pwd ='" + txtpwd.Text + "'";
SqlCommand comm = new SqlCommand(q, connection);
int result = (int)comm.ExecuteScalar();
if(result>0)
{
Session["user"] = txtuname.Text.ToString();
Response.Redirect("home.aspx");
}
else
{
lblerror.Text = "Either username or password in incorrect !";
}
connection.Close();
}
Now put this code on every page load Whenever user login save the username in the session variable and if the session variable is null then redirect user to the login page
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("login.aspx");
}
}