I am using web forms, and the home page (Default.aspx) has login form. Now, if the user logs in, I want the page to reload and show the username (from database) in the text box and disable it. But I don't seems to be able to access the session variable to do that! Even though I can do that when redirecting to other pages.
This is the login button code:
protected void LoginBtn_Click(object sender, EventArgs e)
{
commonMethod.DB_OpenConnection();
var userEmail = login.Text;
var userPass = password.Text;
SqlCommand myCommand = new SqlCommand("SELECT MemberName, Email, Pass, Member_Type FROM Member", commonMethod.myConnection);
SqlDataReader myReader = null;
myReader= myCommand.ExecuteReader();
while(myReader.Read())
{
if ((String)myReader["Pass"] == userPass && (String)myReader["Email"]==userEmail)
{
submitMsg.Text = "success";
Session["username"] = (String)myReader["MemberName"];
Session["role"] = myReader["Member_Type"];
Response.Redirect("Default.aspx");
break;
}
else
{
submitMsg.Text = "Failed";
}
}
commonMethod.DB_CloseConnection();
}
And this is how I am trying to access the session username in the same page.
public string username;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
username = (String)Session["username"];
login.Text = username;
login.Enabled = false;
}
else {
username = "example@gmail.com";
login.Text = username;
}
}
Why can't I access the session variables after logging in !?