2016-07-25 83 views
1

我爲員工創造一個新的登錄頁面,但是遇到一個錯誤無效的嘗試調用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"] = ""; 

} 

它工作正常,爲客戶我的其他登錄頁面。請幫助?我對此很陌生。非常感謝大家!

+6

什麼是'mDB.Close();'循環內部要做什麼,你認爲? (也許你應該在它下面的一行上添加一個'break'。) –

+0

一旦你關閉了循環內部的連接,讀者就會關閉。 –

回答

3

您正在關閉閱讀循環內的連接(使用mDB.Close();)。這根本不是一件好事。爲了解決這個問題,移動千鈞一髮的循環結束之後進行:

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; 


    } //end of if 
} //end of while loop 
mDB.Close(); 
1

您正在關閉中環路的連接,所以rdr.Read()將使用密閉連接,以檢查是否有第二個記錄。那會失敗。

相反,你應該使用using語句來,這確保了連接會即使在發生錯誤的情況下關閉:

using(var mDB = new SqlConnection(connectionString)) 
{ 
    mDB.Open(); 
    using(var cmd = new SqlCommand("SELECT * FROM Employee", mDB)) 
    using(var 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;   
      } //end of if 
     } //end of while loop 
    } 
} 

側面說明:您選擇的所有記錄和過濾在客戶端。相反,你應該WHERE在數據庫中過濾:

using(var cmd = new SqlCommand("SELECT * FROM Employee WHERE [email protected] AND @ePassword=PassWord", mDB)) 
{ 
    cmd.Parameters.Add("@UserName", SqlDbType.Varchar).Value = txtUsername.Text; 
    cmd.Parameters.Add("@ePassword", SqlDbType.Varchar).Value = txtPassword.Text; 
    // ... 
} 
相關問題