我爲員工創造一個新的登錄頁面,但是遇到一個錯誤無效的嘗試調用read時讀取器已關閉錯誤
mDB.Open();
SqlCommand cmd;
SqlDataReader rdr;
String strSql1 = "SELECT * FROM Employee ";
cmd = new SqlCommand(strSql1, mDB);
rdr = cmd.ExecuteReader();
while (rdr.Read() == true)
{
if (txtUsername.Text == (string)rdr["eUserName"] &&
txtPassword.Text == (string)rdr["ePassword"])
{
Session["sFlag"] = "T"; // sFlag = "T" means user has logged in
Session["sFirstName"] = rdr["eFirstName"];
Session["sLastName"] = rdr["eLastName"];
Session["sUsername"] = rdr["eUserName"];
btnLogout.Visible = true;
btnLogin.Visible = false;
mDB.Close();
} //end of if
} //end of while loop
// userid and password not matched, hence login unsuccessful
Session["sFlag"] = "F";
Session["sName"] = "";
Session["sUsername"] = "";
lblMessage.Text = "Username and password does not match- please try again. ";
mDB.Close();
}
protected void btnLogout_Click(object sender, EventArgs e)
{
btnLogout.Visible = false;
Session["sFlag"] = "L"; // L for logout
Session["sFirstName"] = "";
Session["sUsername"] = "";
}
它工作正常,爲客戶我的其他登錄頁面。請幫助?我對此很陌生。非常感謝大家!
什麼是'mDB.Close();'循環內部要做什麼,你認爲? (也許你應該在它下面的一行上添加一個'break'。) –
一旦你關閉了循環內部的連接,讀者就會關閉。 –