Mistake: there is already an open DataReader. ♪



  • Hi, Hash Cod, 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 Data Base and if it's more than zero and more than the numBalace element, make the message that the balance is short... otherwise do the code that's in the other block, but I have a mistake.

    There is already an open DataReader associated with this Connection which must be closed first



  • The problem is... someone who needs a corrected version, hold it.

     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();
        }
    }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2