SqlDataAdapter.Update

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a dataset with with 2 datatables in it. We'll call them the Objects
table and the Assignments table. The primary keys for both tables are
autoincrement fields. I need to write a record to the Objects table, then
write another record to the Assignments table that has a reference to the
record added to the Objects table. I then call SqlDataAdapter.update on the
Objects table, then the Assignments table. The problem is when I look at the
records that were written, the relationship is broken. The Objects record
has a different ID than the original one I wrote to the datatable, thus
breaking the relationship. How do I keep my relationships intact?
 
Do you have a DataRelation object set up on the dataset? That should take
care of it for you.

HOwever I'm confused about the structure, if I create a new field in Object
and it assigns - 20, and I create two children records - those numbers could
be 30, 40 or whatever - so I assume you mean back in the database? If so,
the datarelation should handle it. Update the parent, get back the correct
ID, the change will cascade down, then update the child table.
 
Ok, take the current database structure.

Objects
ID | ObjectName | ObjectDescription
1 | Object1 | This is object 1
2 | Object2 | this is object2

Assignments
ID | Object | Attribute
1 | 1 | 453
2 | 1 | 64354
3 | 2 | 323
4 | 2 | 4534

Objects.ID = Assignments.Object

I add a record in the datatable to Objects, the datatable will assign it an
ID of 3. I add a record to the Assignments table that references ID 3 of the
Objects table.
Now, I want to commit the data to the SQL database. The new Assignments
record is fine, but the new Objects record doesn't always get the same ID as
the one in the datatable. The results may look something like this:

Objects
ID | ObjectName | ObjectDescription
1 | Object1 | This is object 1
2 | Object2 | this is object2
5 | Object3 | this is object3

Assignments
ID | Object | Attribute
1 | 1 | 453
2 | 1 | 64354
3 | 2 | 323
4 | 2 | 4534
5 | 3 | 43242

How would I add a DataRelation to handle this? Do I have to reload the data
to get the correct IDs in memory?
 
Back
Top