You are closing the connection inside loop. It will for first time but next time Database connection is not opened so you are getting exception. Secondly use
ExecuteNonQuery()
instead of
ExecuteReader()
. Try with below code:
try
{
Globals.hardie.Open();
for (int x = 0; x < 11; x++)
{
string command = "UPDATE `james_hardie`.`config` SET `system_name`='" + Globals.printerNames[x] + "' WHERE `idconfig`='" + (x + 1) + "';";
MySqlCommand excute = new MySqlCommand(command, Globals.hardie);
excute.ExecuteNonQuery();
}
Globals.hardie.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Although above one resolve your problem stills it has
SQL Injection problem, so always use parameterised query like below:
MySqlCommand cmd1;
cmd1 = new MySqlCommand("SELECT * FROM User_table WHERE User_ID = @UserID", Globals.hardie);
cmd1.Parameters.AddWithValue("@UserID", "yourValue"));
excute.ExecuteNonQuery();
cmd1.Parameters.Clear();