DataUpdate passing wrong values to SqlServer?!?

  • Thread starter Thread starter Jens Weiermann
  • Start date Start date
J

Jens Weiermann

Hi!

I have a rather simple DataSet with two tables in it (parent / child). I've
set the UpdateRule property of the relation to "Cascade", because this
value is changed when the parent row is written to the sql server (an
identity column); so after I call the Update method of the DataAdapter for
the parent table, the new key values are reflected in the child table
before I finally call the Update method of the DataAdapter for the child
table. So far, so good.
However, I'm experiencing strange problems when Deleting a record from the
child table after it has been inserted. I can see using Sql profiler that
wrong key values are written to the database (causing the update to fail).
Strangely, the DiffGram written to disk just before the Update looks good.

Even more strangely, it works fine when Clear()ing the DataSet and loading
it again from the (just saved) DiffGram.

Anybody have a clue what might happen here?

Thanks!
Jens
 
Hi Jens,

When dealing with autoinc fields, you have to be aware, that Update method
will insert new records instead of updating the original ones. This happens,
because Update just looks at pk value (which are different in this case) and
blindly inserts new values.

The better way would be to:
- create GetChanges dataset
- create pairs of rows for Added rows: original row - getchanged equivalent
rows (store both references)
- do the update on GetChanges
- correct pk of original rows using pairs you've created above
- do the Merge
 
Back
Top