2017-08-21 8 views
-1

次のコードは、if-elseステートメントにメッセージボックスを表示した後に例外をスローします。例外は、それが閉じているので、私の読者は、読むことができないことを言う:while(datareader.Read())の使用方法

try 
{ 
    using (MySqlConnection conn = new MySqlConnection(Config.ConnectionString())) 
    { 
     conn.Open(); 
     MySqlCommand cmd = new MySqlCommand("SELECT * from tb_useraccounts;", conn); 
     MySqlDataReader dr = cmd.ExecuteReader(); 

     while (dr.Read()) 
     { 
      if (UserName == (dr["username"].ToString()) && PassWord == (dr["password"].ToString())) 
      { 
       frmMain main = new frmMain(); 
       main.Show(); 
       login.Hide(); 
      } 
      else if (UserName == string.Empty || PassWord == string.Empty) 
      { 
       MessageBox.Show("Please fill the blank spaces!"); 
      } 
      else 
      { 
       MessageBox.Show("The username and password you inserted is incorrect!"); 
      } 
      dr.Close(); 
      conn.Close(); 
     } 
    } 

どのように私は私のwhileループが正しく動作して作ることができますか?

答えて

0

あなたは怒鳴る

try 
    { 
    using (MySqlConnection conn = new MySqlConnection(Config.ConnectionString())) 
     { 
     conn.Open(); 
    MySqlCommand cmd = new MySqlCommand("SELECT * from tb_useraccounts;", conn); 
    MySqlDataReader dr = cmd.ExecuteReader(); 

    while (dr.Read()) 
    { 
     if (UserName == (dr["username"].ToString()) && PassWord == (dr["password"].ToString())) 
        { 
         frmMain main = new frmMain(); 
         main.Show(); 
         login.Hide(); 
        } 
        else if (UserName == string.Empty || PassWord == string.Empty) 
        { 
         MessageBox.Show("Please fill the blank spaces!"); 
        } 
        else 
        { 
         MessageBox.Show("The username and password you inserted is incorrect!"); 
        } 
       } 
        dr.Close(); 
        conn.Close(); 
      } 
のようなwhileループの後にリーダーとの接続を閉じる必要があります
関連する問題