Question

I have this reoccurring problem with one of the Web Services I'm working on. I am using Transactions and Batch Update using Data Adapters. On top of that I am also using Oracle Command Builder to get my insert/delete/update commands which I assign to the dataadapter.

Here is some extracted code from my service; and with the error line:

Mind it - I'm posting code extracted from my source:

OracleTransaction trx = null;
if ((Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty))
                {
                    con.ConnectionString = con.ConnectionString + ";enlist=false"; ;
                    con.Open();
                    trx = con.BeginTransaction();
                    cmd.Transaction = trx;
                }

                try
                {
                    dsEmpData.AcceptChanges();
                    for (int i = 0; i < dsEmpData.Tables[0].Rows.Count; i++)
                    {
                        dsEmpData.Tables[0].Rows[i].SetAdded();
                    }
                    OracleCommandBuilder cb = new OracleCommandBuilder();
                    string SQL
                            = "--MY SELECT COMMAND. REMOVED BECAUSE IT IS NOT NECESSARY IN THIS BLOCK";

                    OracleDataAdapter da = new OracleDataAdapter(SQL, ConnectionString);
                    OracleCommandBuilder cmdInsert = new OracleCommandBuilder(da);
                    dsEmpData.Tables[0].TableName = "TABLENAME".ToUpper();

                    if ((Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty))
                    {
                        da.SelectCommand.Transaction = trx;
                        da.InsertCommand = cmdInsert.GetInsertCommand();//ERROR LINE
                        da.InsertCommand.Transaction = trx;
                    }
                    da.Update(dsEmpData, "TABLENAME".ToUpper());
                    if (trx != null)
                    { trx.Commit(); }
                }
                catch (Exception ex)
                {
                    if (trx != null)
                    { trx.Rollback(); }
                    throw new FaultException("MY MESSAGE " + ex.Message);
                }

So, as stated above - I get the exception "The transaction object is not associated with the connection object" on the above stated line: da.InsertCommand = cmdInsert.GetInsertCommand();

Any help on this matter will be highly appreciated. If you need more information, kindly let me know.

Thanks in advance.

Was it helpful?

Solution

It's because the transaction belongs to the connection called con, created in about line 5 of your code. However, the OracleDataAdaptor is not associated with that connection. You give it the connection string and not the connection. This means that it will create its own connection.

You then give this adaptor the transaction associated with the previous connection. This won't work! You have to associate the data adaptor with same connection as the transaction. You could either associate trhe adaptor with the original connection, or create the transaction from the adaptor's connection; one or the other will work.

Cheers -

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top