Click here to Skip to main content
15,906,341 members
Home / Discussions / C#
   

C#

 
AnswerRe: Unable to load DLL - Why ?? Pin
Luc Pattyn28-Apr-09 16:31
sitebuilderLuc Pattyn28-Apr-09 16:31 
GeneralRe: Unable to load DLL - Why ?? Pin
matty2desmara29-Apr-09 4:08
matty2desmara29-Apr-09 4:08 
AnswerRe: Unable to load DLL - Why ?? Pin
jas0n2328-Apr-09 23:27
jas0n2328-Apr-09 23:27 
GeneralRe: Unable to load DLL - Why ?? Pin
matty2desmara29-Apr-09 4:20
matty2desmara29-Apr-09 4:20 
AnswerRe: Unable to load DLL - Why ?? Pin
matty2desmara29-Apr-09 4:54
matty2desmara29-Apr-09 4:54 
AnswerRe: Unable to load DLL - Why ?? Pin
S. Senthil Kumar30-Apr-09 9:53
S. Senthil Kumar30-Apr-09 9:53 
QuestionDrag drop from picturebox to desktop? Pin
Jacob Dixon28-Apr-09 13:32
Jacob Dixon28-Apr-09 13:32 
GeneralRate this tricky code : Use multiple TableAdapters within a single transaction! Pin
hdv21228-Apr-09 9:29
hdv21228-Apr-09 9:29 
Hi everybody
after a lot of weeks about update related tables in database in a single transaction and configuring TableAdapters with stored procedures for CRUD operations, and having many trouble with TableAdapterManager.UpdateAll method to do this (see this post), i decide write some tricky code to do this myself and i put this code here for discussion :

Note : i have 2 tables, Customers & Orders. The Customers table is a parent of Orders table



TestDataSet ds = new TestDataSet();

private void toolStripButton1_Click(object sender, EventArgs e)
{            
            if (ds.Customers.GetChanges() != null || ds.Orders.GetChanges() != null)
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.TestConnectionString))
                {
                    SqlTransaction tr = null;
                    try
                    {
                        if (con.State != ConnectionState.Open)
                            con.Open();
                        tr = con.BeginTransaction();
                        if (ds.Customers.GetChanges() != null)
                        {
                            TestDataSetTableAdapters.CustomersTableAdapter customersAdapter = new TransactionalUpdate.TestDataSetTableAdapters.CustomersTableAdapter();
                       
                            // Set connection and transaction of TableAdapter to current connection and transaction (con,tr)
                            customersAdapter.Connection = con;
                            customersAdapter.Adapter.InsertCommand.Transaction
                            = customersAdapter.Adapter.UpdateCommand.Transaction
                            = customersAdapter.Adapter.DeleteCommand.Transaction = tr;
                            customersAdapter.Update(ds.Customers.GetChanges() as TestDataSet.CustomersDataTable);
                        }
                        if (ds.Orders.GetChanges() != null)
                        {
                            TestDataSetTableAdapters.OrdersTableAdapter ordersAdapter = new TransactionalUpdate.TestDataSetTableAdapters.OrdersTableAdapter();
                        
                            // Set connection and transaction of TableAdapter to current connection and transaction (con,tr)
                            ordersAdapter.Connection = con;
                            ordersAdapter.Adapter.InsertCommand.Transaction
                            = ordersAdapter.Adapter.UpdateCommand.Transaction
                            = ordersAdapter.Adapter.DeleteCommand.Transaction
                            = ordersAdapter.Adapter.DeleteCommand.Transaction = tr;
                            ordersAdapter.Update(ds.Orders as TestDataSet.OrdersDataTable);
                        }
                        ds.Customers.AcceptChanges();
                        ds.Orders.AcceptChanges();
                        tr.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        tr.Rollback();
                    }
                    finally
                    {
                        if (con.State != ConnectionState.Closed)
                            con.Close();
                    }
                }
            }
}


what about u ?
Is this code good ?
do u have idea to optimize this code ?
Thanks.
GeneralRe: Rate this tricky code : Use multiple TableAdapters within a single transaction! Pin
Henry Minute28-Apr-09 10:49
Henry Minute28-Apr-09 10:49 
GeneralRe: Rate this tricky code : Use multiple TableAdapters within a single transaction! Pin
hdv21228-Apr-09 11:51
hdv21228-Apr-09 11:51 
QuestionReading a Konica Minolta printer to get print & copy counts Pin
crazylad28-Apr-09 9:19
crazylad28-Apr-09 9:19 
AnswerRe: Reading a Konica Minolta printer to get print & copy counts Pin
Henry Minute28-Apr-09 10:51
Henry Minute28-Apr-09 10:51 
GeneralRe: Reading a Konica Minolta printer to get print & copy counts Pin
crazylad6-May-09 5:08
crazylad6-May-09 5:08 
NewsGetting the server name and SQL instance name [modified] Pin
swetha sankaran28-Apr-09 9:10
swetha sankaran28-Apr-09 9:10 
QuestionRe: Getting the server name and SQL instance name Pin
Jimmanuel28-Apr-09 10:37
Jimmanuel28-Apr-09 10:37 
GeneralRe: Getting the server name and SQL instance name Pin
Rolando CC28-Apr-09 11:00
professionalRolando CC28-Apr-09 11:00 
QuestionFormatting Pin
bwood202028-Apr-09 8:54
bwood202028-Apr-09 8:54 
AnswerRe: Formatting Pin
snorkie28-Apr-09 9:28
professionalsnorkie28-Apr-09 9:28 
GeneralRe: Formatting Pin
bwood202028-Apr-09 10:35
bwood202028-Apr-09 10:35 
GeneralRe: Formatting Pin
snorkie30-Apr-09 9:47
professionalsnorkie30-Apr-09 9:47 
GeneralRe: Formatting Pin
bwood202030-Apr-09 10:30
bwood202030-Apr-09 10:30 
QuestionSecure File Transfer with C# Pin
SimpleData28-Apr-09 8:12
SimpleData28-Apr-09 8:12 
QuestionBuild a text control Pin
Alquerque28-Apr-09 6:54
Alquerque28-Apr-09 6:54 
AnswerRe: Build a text control Pin
Giorgi Dalakishvili28-Apr-09 7:00
mentorGiorgi Dalakishvili28-Apr-09 7:00 
GeneralRe: Build a text control Pin
Alquerque28-Apr-09 7:18
Alquerque28-Apr-09 7:18 

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

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