So, I want to check if the table exists in database and if it exists it has to throw a MessageBox. My problem is, when I execute the Reader I'm unable to close it and it gives the following error:
There is already an open DataReader associated with this Connection which must be closed first.
string query2 = $"SHOW TABLES WHERE Tables_in_appdb LIKE '%{tableNamee}%'";
var conn = new MySqlConnection(dbConnectionString);
conn.Open();
var cmd2 = new MySqlCommand(query2, conn);
var reader = cmd2.ExecuteReader();
if (reader.Read())
{
ia.flag = "stop";
cmd2.Cancel();
reader.Close();
}
else
{
cmd.ExecuteNonQuery();
ia.flag = "continue";
cmd2.Cancel();
reader.Close();
}
conn.Close();
return true;
I'm using the reader.Close()
but it seems like I have to do something else. Any ideas?
p.s.: the "ia.flag" is just a string I want to pass to other class if the table exists or not.
EDIT This works:
string query2 = $"SHOW TABLES WHERE Tables_in_appdb LIKE '%{tableNamee}%'";
using (var conn = new MySqlConnection(dbConnectionString))
{
conn.Open();
var cmd = new MySqlCommand(query, conn);
//using(var cmd1 ) ..)
using (var cmd2 = new MySqlCommand(query2, conn))
{
using (var reader = cmd2.ExecuteReader())
{
if (reader.Read())
{
reader.Close();
MessageBox.Show("Ime ankete vec postoji, odaberite drugo!");
flag = false;
conn.Close();
return false;
}
else
{
reader.Close();
flag = true;
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
}
}
}