I have the following script to fetch me some data from a database and then remove it:
public void checkDB()
{
string query = "SELECT * FROM dbt";
SqlCommand sqlCommand = new SqlCommand(query, conn);
SqlDataReader reader;
int id = -1;
using (reader = sqlCommand.ExecuteReader())
{
if (reader.Read())
{
String data= reader["sdata"].ToString();
Order o = new Order(reader["sdata"].ToString());
o.prepareForScript();
id = reader.GetSqlInt32(1).Value;
}
reader.Close();
}
if (id != -1)
{
string removeQuery = "DELETE FROM data WHERE ID=" + id;
SqlCommand removeCMD = new SqlCommand(removeQuery, conn);
removeCMD.ExecuteNonQuery();
}
}
This code results in an exception
unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
with the aditional information that a reader is already associated with this connection. However as you can see the reader is both closed and inside a using loop meaning that it should definitly be closed. Anybody know how to fix this?