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.