Hi HashCode, I have a code

private void btnAddAdv_Click(object sender, EventArgs e) { if (textUrl.Text == "" || numBalance.Value == 0) { MessageBox.Show("Заполните все поля", "Системное сообщение"); } else { MySqlConnection cnt = new MySqlConnection(strProvider); try { cnt.Open(); MySqlCommand command = cnt.CreateCommand(); command.CommandText = "SELECT * FROM t_user WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { if (Convert.ToInt32(reader["balance"]) <= 0 && Convert.ToInt32(reader["balance"]) < Convert.ToInt32(numBalance.Value)) { MessageBox.Show("Недостатовно средств", "Системное сообщение"); } else { MySqlCommand balancecmd = cnt.CreateCommand(); balancecmd.CommandText = "SELECT balance FROM t_user WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; balancecmd.ExecuteNonQuery(); MySqlCommand cmd = cnt.CreateCommand(); cmd.CommandText = "UPDATE t_user SET balance = balance - 50 - '" + numBalance.Value + "' WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; cmd.ExecuteNonQuery(); MessageBox.Show("Сайт добавлен", "Системное сообщение"); } } cnt.Close(); reader.Close(); } catch (MySqlException ee) { MessageBox.Show(ee.Message, "Системное сообщение"); } } 

This code should compare the value from the Database and if they are greater than zero and greater than the value of the numBalace element, display a message that the balance does not have enough funds ... otherwise, make that code in the else block, but I have an error

There is already an open DataReader associated with this Connection.

Reported as a duplicate by Pavel Mayorov c # 21 Sep '17 at 11:10 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • try to close the connection after the catch of the block - Gorets
  • The same thing: ( - Angus123

2 answers 2

I solved the problem ... who needs a corrected version, keep

  private void btnAddAdv_Click(object sender, EventArgs e) { if (textUrl.Text == "" || numBalance.Value == 0) { MessageBox.Show("Заполните все поля", "Системное сообщение"); } else { MySqlConnection cnt = new MySqlConnection(strProvider); try { cnt.Open(); MySqlCommand command = cnt.CreateCommand(); command.CommandText = "SELECT * FROM t_user WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { if (Convert.ToInt32(reader["balance"]) <= 0 && Convert.ToInt32(reader["balance"]) < Convert.ToInt32(numBalance.Value)) { MessageBox.Show("Недостатовно средств", "Системное сообщение"); } else { MySqlCommand balancecmd = cnt.CreateCommand(); balancecmd.CommandText = "SELECT balance FROM t_user WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; MySqlCommand cmd = cnt.CreateCommand(); cmd.CommandText = "UPDATE t_user SET balance = balance - 50 - '" + numBalance.Value + "' WHERE username='" + textUserName.Text + "' and pass='" + Convert.ToString(textPassword.Text) + "'"; reader.Close(); cmd.ExecuteNonQuery(); balancecmd.ExecuteNonQuery(); MessageBox.Show("Сайт добавлен", "Системное сообщение"); } } } catch (MySqlException ee) { MessageBox.Show(ee.Message, "Системное сообщение"); } cnt.Close(); } } 

    It’s better to always use a similar thing.

     using (cnt) { cnt.Open(); // ... } 

    This ensures that after each call to the connection it will be closed. And it is better not to use global variables, but to create a local

     using (var connection = new SQLCinnection(_connectionString)) { connection.Open(); // ... }