There are plenty of people talking about it online, but this just doesn't seem to work. This is the exception that I get:
This SqlTransaction has completed; it is no longer usable.
Here is the code
using (SqlConnection locationConnection = new SqlConnection(connectionString))
{
locationConnection.Open();
SqlTransaction transaction = locationConnection.BeginTransaction();
SqlCommand cmd = new SqlCommand("
Select stuff from table A
Insert stuff into table B
Delete stuff from table A", locationConnection, transaction);
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//Doesn't matter
}
}
//Exception happens here
transaction.Commit();
}
Can anyone shed light on why this is happening? If I move the commit inside the scope of SqlDataReader I get the exception that the datareader needs to close first.
EDIT: I've answered my question and will try to remember to come accept it in a couple days when I'm allowed to.