Feb 08, 2010 08:40 PM|marko.pa|LINK
I'm getting "This SqlTransaction has completed; it is no longer usable" exception when try to commit my transaction after sqlreader is close.
Here is the code sample
SqlTransaction transaction = null;
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "some string"
Command = new SqlCommand();
if (connection.State == ConnectionState.Closed)
transaction = connection.BeginTransaction();
Command.Connection = connection;
Command.Transaction = transaction;
SqlParameter p_id = new SqlParameter("@id", id);
Command.CommandText = "select * from table where id = @id";
Command.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = Command.ExecuteReader();
if (reader != null)
bool result = reader.HasRows;
catch (Exception e)
if (transaction != null)
if (connection.State == ConnectionState.Open)
...so when I get to commit the transaction it raises the mentioned exception: "This SqlTransaction has completed; it is no longer usable". I have also noticet that Command.Transaction becomes NULL after reader.Close()
My question is: Can I use SqlDataReader and SqlTransaction ? Maybe to use BeginExecuteReader and EndExecuteReader ?
If anyone can help me to fix the code...