2017-10-20 54 views
0

我有一個登錄表單。我的查詢在我的數據庫中運行並交叉檢查輸入的參數是否與數據庫中的參數匹配,然後將您重定向到頁面。我在代碼中添加了一個條件來在連接打開或關閉時捕獲異常。我試圖尋找像這樣的解決方案one和其他人。ExecuteReader需要一個開放且可用的連接,但連接已打開且不起作用

我已經完成了以下工作。

  • 包括usings

但是其仍顯示出在運行時出現此錯誤

ExecuteReader需要一個開放和可用的連接。連接的當前狀態已關閉。

這是我的代碼:

public void LWAPLogin(string username, string password) 
    { 
     string wrongCredentials = "Username does not exist. Or password is incorrect"; 
     string query = "Select Username, Password from LWAP where [email protected] AND [email protected];"; 
     using (SqlCommand command = new SqlCommand(query, Connect.con)) 
     { 
      command.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = username; 
      command.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = password; 

      try 
      { 
       if (connection.con.State == ConnectionState.Open) 
       { 
        using (SqlDataReader dr = command.ExecuteReader()) 
        { 
         if (dr.Read()) 
          Response.Redirect("LWAPHome.aspx"); 
         else 
          ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + wrongCredentials + "');", true); 

         dr.Close(); 
        } 
        connection.con.Close(); 
       } 


       else if (connection.con.State == ConnectionState.Closed) 
       { 
        connection.con.Open(); 


        using (SqlDataReader dr = command.ExecuteReader()) 
        { 
         if (dr.Read()) 
          Response.Redirect("LWAPHome.aspx"); 
         else 
          ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + wrongCredentials + "');", true); 
         dr.Close(); 
        } 
        connection.con.Close(); 

       } 
      } 
      finally 
      { 
       //connection.con.Open(); 
      } 
     } 

    } 

從老鄉程序員得到提醒後。我改變了我的代碼,問題得到解決。但是現在我有一個新問題。當一切正常時,它假設重定向到新頁面,但它不是。只是將頁面url更改爲所需的頁面,但仍保留在登錄頁面上。

下面是我的編輯代碼:

public void LWAPLogin(string username, string password) 
    { 
     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["tlcString"].ConnectionString)) 
     { 
      string wrongCredentials = "Username does not exist. Or password is incorrect"; 
      string query = "Select Username, Password from LWAP where [email protected] AND [email protected];"; 
      using (SqlCommand command = new SqlCommand(query, con)) 
      { 

       command.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = username; 
       command.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = password; 

       con.Open(); 

       using (SqlDataReader dr = command.ExecuteReader()) 
       { 
        if (dr.Read()) 
         Response.Redirect("LWAPHome.aspx"); 
        else 
         ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + wrongCredentials + "');", true); 

        dr.Close(); 
       } 
       //connection.con.Close(); 
      } 
      con.Close(); 

     } 
    } 

請參閱image它現在顯示的錯誤。我嘗試了使用不同的瀏覽器,但他們都做同樣的事情。

+0

爲什麼重用'SqlConnection'呢?這導致了這樣的問題。相反,使用'using'語句在每種方法中創建並使用它。可能相關:[ExecuteReader需要一個開放和可用的連接。連接的當前狀態是連接](https://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren) –

+0

你可能會遇到一個問題連接可能會關閉...所以它不關閉.. – BugFinder

+0

因此,我應該使用一個使用和聲明連接內使用?我是否應該繼續調整我的查詢,否則它將變得不必要? –

回答

1

您正在創建一個新的SqlCommand(查詢,Connect.con))指定Connect.con爲使用的連接,而你檢查connection.con,不同的SqlConnection實例的狀態。如果你真的想檢查任何東西,確保你正在檢查正確的東西。

+0

我解決了這個問題,問題仍然存在 –

+0

不,問題沒有持續。最初提出的問題得到了回答。您的代碼中還有其他問題。堆棧溢出不是讓你的代碼完全被調試,而是通過這樣的方式來回答一個問題,其他人可能從這個問題中受益,這是答案。通過改變這個問題,你可以模糊原來的問題並且達到這個目的。正如Tim Schmelter所說,請將此問題標記爲已回答併爲您的下一個問題創建一個新問題。 – rrozema

相關問題