How to save a DataTable using a DataTableAdapter

Any hints on why this doesn't save any records to the database table?

VrsSourceTableAdapters.DataTableCustomerTableAdapter customerAdapterS =
                    new VrsSourceTableAdapters.DataTableCustomerTableAdapter();
                VrsSource.DataTableCustomerDataTable customersS;
                VrsTargetTableAdapters.DataTableCustomerTableAdapter customerAdapterT =
                    new VrsTargetTableAdapters.DataTableCustomerTableAdapter();
                VrsTarget.DataTableCustomerDataTable customersT;
                customersS = customerAdapterS.GetDataVrsCustomers();
                customersT = customerAdapterT.GetDataVrsCustomers();
                foreach (VrsSource.DataTableCustomerRow customerRowS in customersS)
                    Console.WriteLine("Customer: " + + "");
                    Console.WriteLine("  CustomerNo: " + customerRowS.customerNo + "");
                Console.WriteLine("s: " + customersS.Count.ToString());
                Console.WriteLine("t: " + customersT.Count.ToString());
                foreach (VrsTarget.DataTableCustomerRow customerRowT in customersT)
                    Console.WriteLine("TCustomer: " + customerRowT.Name + "");
                    Console.WriteLine("  TCustomerNo: " + customerRowT.CustomerNo + "");
                    Console.WriteLine(" Error: " + customerRowT.HasErrors.ToString());

Open in new window

LVL 54
Gustav BrockCIOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOAuthor Commented:
I should add, that the purpose is to read records from one database/table to another database/table row by row as some conditions (not shown) are to be met for a row to be copied.

Target engine is SQL Server 2005.
IDE: VS2005 Standard.

For my self i would use a dataset wich you would fill with your table adapter.
When finished copying from one table to the others from the dataset use accept change and update from from the dataset.  For me it's work perfectly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOAuthor Commented:
So you fill a DataSet and not a DataTable?
And then call AcceptChanges and then update of the DataTableAdapter?

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

yes, on your dataset you will fill the date associate with it.


This will update entire dataset.
You can do it on specific table in it too.

Gustav BrockCIOAuthor Commented:
That would be nice but also represents the problem - I can't find an Update method for neither a DataSet nor a DataTable.

Only for the DataTableAdapter it seems to exist and that "doesn't work" (no errors, but no appending of records) for me with a DataTable.
Now I'll try to work with a DataSet ...

Gustav BrockCIOAuthor Commented:
Oh well, make a note on this should you ever wish to copy data between DataSets or DataTables with method ImportRow.

The secret is the value of property RowState of the individual rows. Rows read from a table have a RowState of Unchanged.
If a row is copied to another DataSet/DataTable by method ImportRow, RowState remains Unchanged.

In my code sample, I marked rows as Modified with method SetModified.

The Update method of the DataTableAdapter, which is called to write the data rows to the underlying table, is quite clever as it doesn't touch rows with RowState Unchanged.
However, rows with RowState Modified will only be used to update existing records in the target table, thus it will not attempt to append or insert any such row.

For a row to be inserted by the later Update call, it has to have RowState set to Added.

So, for my code below, I have to decide if a row should update an existing record, or if it should be appended, or if it should be ignored - and then call the method SetModified or SetAdded or nothing:

                    // Mark row as modified.
                    // Mark row as new.

When I changed the code from SetModified to SetAdded and replaced the Fill method with:


all records were written to the target table.

Of course, if you can write your code to add rows not using method ImportRow - with LoadDataRow or like:


RowState of those rows, I guess, will be switched to Added automatically.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.