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.
It seems that solved my problem. Thank you very much.
– DLopes
If it was helpful @Dlopes accepted as response!
– novic