Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends


In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to apply changes in a DataTable back to its table source in SQL Server. It worked fine when DataSet had only 1 table.

This time I have 2 tables in a dataset and I want to update them all at once. However it only updates the first one and fails when attempts to update 2nd table.. Has anyone done anything like that?

It looks like SqlCommandBuilder generates insert command only for the first table.


Thanks for the help.
Posted
Updated 1-Dec-11 23:47pm
v2
Comments
D K N T H 2-Dec-11 4:13am    
provide your code for immediate response.. thnks

I think this article[^] might help you. It's part of a set of articles[^]. And that too is part of more articles[^].

An excerpt from the first article:
If your dataset contains multiple tables, you have to update them individually by calling the Update method of each data adapter separately. If the tables have a parent-child relationship, it is likely that you will have to send updates to the database in a particular order. A common scenario is that you have added both parent and related child records to a dataset — for example, a new customer record and one or more related order records. If the database itself is enforcing relational integrity rules, it will raise errors if you send the new child records to the database before the parent record has been created.

Conversely, if you delete related records in the dataset, you generally have to send updates in the reverse order: child table first, then parent table. Otherwise, the database is likely to raise an error because referential integrity rules will prevent you from deleting a parent record while related child records still exist.

A general rule for sending updates for related tables is to follow this order:

Child table: delete records.
Parent table: insert, update, and delete records.
Child table: insert and update records.
Hope it helps :)
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900