TableAdapter not updating identity

349 views Asked by At

I use a TableAdapter to update the database like this:

var changes = anpDatabase.AnpCollection.GetChanges();
anpCollectionTableAdapter.Update((AnpDatabase.AnpCollectionDataTable)changes);
anpDatabase.AnpCollection.Merge(changes);
anpDatabase.AnpCollection.AcceptChanges();

However this isn't enough to update the identity primary column... I tried putting changing the Insert Command of the adapter to the following:

INSERT INTO AnpCollection
                  (ParentCollectionId, StartDate, EndDate, Completed, IsPartial, Partitioned, IsAutomaticallyGenerated)
VALUES     (@ParentCollectionId,@StartDate,@EndDate,@Completed,@IsPartial,@Partitioned,@IsAutomaticallyGenerated);
SELECT CollectionId, ParentCollectionId, StartDate, EndDate, Completed, IsPartial, Partitioned, IsAutomaticallyGenerated FROM AnpCollection WHERE (CollectionId = SCOPE_IDENTITY())

However, If I do this, instead of the column being updated, a new row appears with the correct identity value... That's not what I want... Can someone help me? Thanks in advance!

1

There are 1 answers

0
Mike Perrenoud On BEST ANSWER

Leaving the INSERT statement with the SELECT at the end, it seems like the right code here is:

anpCollectionTableAdapter.Update(anpDatabase.AnpCollection);

instead of:

var changes = anpDatabase.AnpCollection.GetChanges();
anpCollectionTableAdapter.Update((AnpDatabase.AnpCollectionDataTable)changes);
anpDatabase.AnpCollection.Merge(changes);
anpDatabase.AnpCollection.AcceptChanges();