SqlTransaction does not work

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

Hi,
I have a sqlDataAdapter that has a insertcommand and an
updatecommand.

I have initialized the connection to the insertCommand and
updateCommand as mySqlConnection( that is the connection property of
the class ). mySqlDataAdapter is also a property of the object

example

myclassConstructor()
{
mySqlCOnnection = new SqlConnection( myConnString );
}

I use this connection object to init a transaction in another method

example

private void DoThat()
{
SqlTransaction newTransaction = mySqlConnection.BeginTransaction();

//.. init the trans for the command
mySqlDataAdapter.InsertCommand.Transaction = newTransaction;

// ... and then say
mySqlDataAdapter.Update();

newTransaction.Commit();

}

this does not work. I always get the error message saying "Transaction
object not associated with the connection object"

But when I explicitly say
mySqlDataAdapter.InsertCommand.Connection = mySqlConnection;

in the DoThat method, it succeeds. I dont understand that. I have
checked everything with the connection object and nothing seems to be
different.

Can someone help me on this,
TIA,
Raj
 
Hi Raj,

As you've already discovered, you have to assign both connection and
transaction (that is initialized from connection) to each command used in
adater.
 
Back
Top