Click here to Skip to main content
11,411,922 members (52,739 online)
Click here to Skip to main content

Using TableAdapters with transactions

, 18 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Use TableAdapters with transactions.


How can we update two TableAdapters in one transaction? I really was surprised to have so much trouble with this issue, which I assumed would be a basic functionality.

I will use the Northwind database as an example. I want to add a new record to the Order and Order Details tables. I want to do this using a transaction. I do not want to add orders without order details. And, I want to use TableAdapters.


Well, the problem is that TableAdapters do not expose a Connection property or Transaction property, so how am I going to do this?! Beats me! Luckily, Reflection, Google, and Ryan Whitaker did save my day(s).


  • 0. Initialize connection, begin transaction.
  • 1. Use Reflection to access the SqlDataAdapter of the TableAdapter(s) on which you want to use the transaction.
  • 2. Set this adapter's Insert/Update/Delete Connection property to the connection which did create the transaction.
  • 3. Set this adapter's Insert/Update/Delete Transaction property to the appropriate transaction.
  • 4. Do this for each TableAdapter you need to update in the transaction.
  • 5. Commit the transaction.

In other words:

conn = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString);
trans = conn.BeginTransaction();
public SqlDataAdapter GetAdapter(object tableAdapter)
    Type tableAdapterType = tableAdapter.GetType();
    SqlDataAdapter adapter = (SqlDataAdapter)tableAdapterType.GetProperty("Adapter", 
           BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableAdapter, null);
    return adapter;
adapter.InsertCommand.Connection = trans.Connection;
adapter.UpdateCommand.Connection = trans.Connection;
adapter.DeleteCommand.Connection = trans.Connection;

adapter.InsertCommand.Transaction = trans;
adapter.UpdateCommand.Transaction = trans;
adapter.DeleteCommand.Transaction = trans;



Download the attached project for a working example.

Make sure you change this line of code to your appropriate database connection:

Properties.Settings.Default.NorthwindConnectionString = 
   @"Data Source=kenny2;Initial Catalog=Northwind;Integrated Security=True";


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Danko Greiner

Croatia Croatia
No Biography provided

Comments and Discussions

GeneralThank you!!! PinmemberJamesHG13-Apr-10 7:24 
GeneralThanx Pinmembermohammad16-Mar-08 8:55 
GeneralSCOPE_IDENTITY Not rolling back Pinmemberpaulvzyl31-May-07 1:16 
GeneralRe: SCOPE_IDENTITY Not rolling back PinmemberDanko Greiner31-May-07 2:32 
GeneralRe: SCOPE_IDENTITY Not rolling back Pinmemberpaulvzyl31-May-07 5:04 
Questionpartial class PinmemberBoudino23-May-07 4:39 
AnswerRe: partial class PinmemberDanko Greiner23-May-07 5:24 
GeneralRe: partial class PinmemberBoudino23-May-07 9:00 
GeneralBtw... Pinmemberfadee20-May-07 3:47 
Generaleasy way... Pinmemberfadee20-May-07 3:46 
GeneralRe: easy way... PinmemberDanko Greiner20-May-07 22:30 
GeneralRe: easy way... Pinmemberfadee21-May-07 1:02 
QuestionRe: easy way... PinmemberPreston McCormick26-Jun-07 15:55 
AnswerRe: easy way... PinmemberAlexander Myachin10-Aug-07 20:52 
GeneralI have another way... PinmemberShipcreak22-Feb-07 11:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 18 Feb 2007
Article Copyright 2007 by Danko Greiner
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid