Any Easy method to update Table in Dataset

gprabaka

Newcomer
Joined
Jul 14, 2005
Messages
21
Good Afternoon,
I have a DataSet ds. This contains 2 Tables, Table1 and Table2.

I display Table1 on a Grid and Table 2 on another Grid. Table1 is the parent of Table2. When I create a new row on the Grid for Table1, I go to the database to insert the new Table1 row into the Table and get the Table1ID.
For this I define a DataTable dtChanges = ds.Tables["Table1"].GetChanges() and call a stored procedure with table dtChanges. The ID is blank in ds.Tables["Table1"]. However, after my insert into the database I have the real ID. What is the easiest way for me to update ds.Tables["Table1"] with the ID?

Thanks
Guha
 
You need a few things.

1. You need to have the identity (Table1ID) for Table1 set to increment. This means each new row will have a new number.

2. You need to make sure there is a relationship between both tables and the changes are set to cascade.

This means you have to insert/update table1 prior to table2 and delete in the opposite order.

You need the relationship there because if the database is assigning an identity itself, it's not going to be whatever your dataset or you come up with. So when you insert the Table1 Rows, you'll get a new identity when the data refreshes (which should be on an insert really, but depends on the SQL) and when that new identity comes in, it'll cascade to the Table2 foreign keys.

Also watch out when you're creating new datatables and copying results and possibly merging things. Huge headache. Even though it's a new object, it still keeps references and causes problems if you don't copy/merge properly. It's all rather counter intuitive as are a few things in a dataset.
 
Back
Top