DataAdapter.Fill & AutoIncrement -> ConstraintException

  • Thread starter Thread starter James Cadd
  • Start date Start date
J

James Cadd

DataLayer's steps:
1. DataAdapter.Update(dtUserChanges)
2. DataAdapter.Fill(dtUserChanges)

I'm having trouble with DataAdapter.Fill creating
duplicate rows in a table which has an AutoIncrement
primary key. When it happens, I get a
ConstraintException thrown from the Fill method. I know
it's happening because, to keep the DataSet mergeable, I
handle DataAdapter.RowUpdating and set
sqlRowUpdatedEventArgs.Status =
UpdateStatus.SkipCurrentRow. What's a good way to deal
with this problem?
 
Ok, I fixed this by handling the DataTable.RowChanging
event and throwing an exception if a row was added with a
primary key value that already exists in the DataTable
(to prevent the row from being added). Is there a better
way?? It would be nice if DataAdapter.Fill could just
look at the current versions of the rows instead of the
original versions. Any guidance would be appreciated, I
feel like I've found enough rope to hang myself with!
 
Back
Top