2017-10-20 32 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'ステートメントを使用して、すべてのメソッドでそれを作成してintiliazeします。おそらく関連しています:[ExecuteReaderはオープンで利用可能な接続が必要です。接続の現在の状態は[接続中]です(https://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren) –

+0

おそらくタイムリーな問題が発生しています接続が閉じている可能性がありますので、閉じていません。 – BugFinder

+0

だから私は使用する必要があります使用して、使用内で接続を宣言しますか?そして私はまだ私の質問を条件付けするか、それとも不要になるでしょうか? –

答えて

1

あなたはconnection.con、異なるSqlConnectionオブジェクトインスタンスの状態をチェックしているのに対し、あなたは、使用する接続としてConnect.conを指定して新しいSqlCommandオブジェクト(クエリ、Connect.con))を作成しています。本当に何かをチェックしたいのであれば、正しいものをチェックしていることを確認してください。

+0

私はそれを解決し、問題は依然として続きました –

+0

いいえ、問題は解決されませんでした。最初に提起された質問に答えました。あなたのコードには他の問題もあります。スタックオーバーフローは、完全にデバッグされたコードを取得することではなく、他の人が質問から恩恵を受けるような方法で回答することです。質問を変更することで、元の質問があいまいになり、この目的を破ることができます。ティム・シュメルターが指摘したように、この質問に答えを記入し、次の問題のために新しい質問を作成してください。 – rrozema

関連する問題