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

Asked

Viewed 5,608 times

3

I have a combobox that is receiving data from a table of my BD Mysql. So far so good.

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

I already searched and did what solved the third party problem (create a second connection), but it did not work very well. Follow code method:

 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;

}

Combobox code receiving the data:

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

Observing: found this link, but it didn’t help me much:

There is already an open Datareader associated with this command that must be closed first.

1 answer

4


It lacked calling the method Dispose() and close the connection to the bank, really every time you use a IDataReader need to close right after its use and give the command Dispose() to free up 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;
}

Observing:

In the SQL Server 2005 and later, there is a way to configure the connection to work with several Idatareader open, which is to configure the resource MARS - Multiple Active Result Sets as follows: in string of connection add the configuration:

MultipleActiveResultSets=True;

Example:

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

References

  • 1

    It seems that solved my problem. Thank you very much.

  • If it was helpful @Dlopes accepted as response!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.