Is there already an open DataReader associated with this command that should be closed first?

3

I have combobox that is receiving data from a table in my MySQL DB. So far so good.

However, when I return to the initial screen, where it is, it ends up giving a problem. (There is already a% open% associated with this command that must be closed first.)

I've already tried and done what solved the third-party problem (create a second connection), but it did not work very well. Here is the method code:

 public DataTable GetUF()
 {

        DataTable dataUf = new DataTable();
        MySqlConnection bdConn = new MySqlConnection("Persist Security Info=False;server=localhost;database=controle;uid=root;pwd=''");
        try
        {
            bdConn.Open();
            if (bdConn2.State == ConnectionState.Open)
            {
                //Se estiver aberta faz a consulta dos dados do BD
                MySqlCommand cmd = new MySqlCommand("SELECT identificador FROM computador WHERE status=0", bdConn);
                cmd.BeginExecuteNonQuery();
                dataUf.Load(cmd.ExecuteReader());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Impossível estabelecer conexão.\n" + ex.Message);
        }

        return dataUf;

}

Code of the ComboBox that is receiving the data:

public Form1()
    {
        InitializeComponent();
        cbComputador.ValueMember = "identificador";
        cbComputador.DisplayMember = "identificador";
        cbComputador.DataSource = GetUF();
    }

Note: I found this DataReader , but it did not help me much:

  

#

There is already an open DataReader associated with this command that should be closed first.

    
asked by anonymous 02.06.2017 / 23:16

1 answer

4

The method Dispose() failed to call close the connection to the bank , actually every time you use an IDataReader needs to close shortly after its use and give the command Dispose() to free resources.

public DataTable GetUF()
{
    DataTable dataUf = new DataTable();
    MySqlConnection bdConn 
        = new MySqlConnection(
        "Persist Security Info=False;server=localhost;database=controle;uid=root;pwd=''");
    try
    {
        bdConn.Open();
        if (bdConn2.State == ConnectionState.Open)
        {
            MySqlCommand cmd = new MySqlCommand(
                           "SELECT identificador FROM computador WHERE status=0", bdConn);
            dataUf.Load(cmd.ExecuteReader());
            cmd.Dispose();
        }
        bdConn.Close();
        bdConn.Dispose();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Impossível estabelecer conexão.\n" + ex.Message);
    }
    return dataUf;
}

Note:

In the SQL Server 2005 and higher versions, there is a way to configure the connection to work with multiple IDataReader , which is to set up the MARS - Multiple Active Result Sets as follows: in% with connection add configuration:

MultipleActiveResultSets=True;

Example:

connectionString="Data Source=.\SQLEXPRESS;" + 
             "Initial Catalog=Banco;Integrated Security=True;" + 
             "MultipleActiveResultSets=true;"

References

02.06.2017 / 23:24