3

it's been a long time since the last time I played with TableAdapter I want to use transactions, but that code generates an error

Element ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction phase. Transaction property of the command has not been initialized.

myAdapter.Connection.Open();
using (SqlTransaction trans = myAdapter.Connection.BeginTransaction())
{
    try
    {
        int result = myAdapter.Insert(1,2,3,4,5); //an example
        trans.Commit();
    }
    catch (Exception exc)
    {
        trans.Rollback();
        MessageBox.Show("error");
    }
 }
 myAdapter.Connection.Close();

how to fix it ?

EDIT

fixed by adding:

myAdapter.Adapter.InsertCommand.Transaction = trans;
Tony
  • 12,405
  • 36
  • 126
  • 226

0 Answers0