Merge is adding 2 records to the dataset

  • Thread starter Thread starter Hemang Shah
  • Start date Start date
H

Hemang Shah

What im doing:

Adding new record to my form.

Creating a temp dataset "dsChanges" by using getchanges()

dataadapter.update(dsChanges)

Then I merge my original dataset dsOriginal.merge(dsChanges)

But now my dataset has 2 records instead of 1,

one with the auto id field = -1 and one with the autoid field as the one
retrived from the database.

The database has only 1 record added though.(which is correct).

Anyone got any inputs on this one ?

Thanks
 
Hi Hemang,

Yes, this is how Merge works - it looks at primary key to find out whether
row exists or not and it is not very Update friendly.
In autoinc case it thinks that there is a new row since the primary keys
differ.
This is how I solve it:
Before doing update I link same rows from original and getchanges dataset (I
create an array of instances of a class that holds references to both rows)
After update I manually refresh the primary key of original rows from the
updated rows (first setting DataColumn.ReadOnly = false, refresh the value
and reset ReadOnly).
And then, I do the merge.
 
What if i'm doing only 1 record update at a time.

Also my insert command also has a select statement which uses the @@Identity
to get the auto # assigned to the new row.

Are you saying I update the "dsOriginal" from "dsGetchanges" and then merge?

Then why merge at all ?

Do you have a code snippet I can use ? or any article/book you can point me
to.

Thanks.

HS
Miha Markic said:
Hi Hemang,

Yes, this is how Merge works - it looks at primary key to find out whether
row exists or not and it is not very Update friendly.
In autoinc case it thinks that there is a new row since the primary keys
differ.
This is how I solve it:
Before doing update I link same rows from original and getchanges dataset
(I create an array of instances of a class that holds references to both
rows)
After update I manually refresh the primary key of original rows from the
updated rows (first setting DataColumn.ReadOnly = false, refresh the value
and reset ReadOnly).
And then, I do the merge.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Hemang Shah said:
What im doing:

Adding new record to my form.

Creating a temp dataset "dsChanges" by using getchanges()

dataadapter.update(dsChanges)

Then I merge my original dataset dsOriginal.merge(dsChanges)

But now my dataset has 2 records instead of 1,

one with the auto id field = -1 and one with the autoid field as the one
retrived from the database.

The database has only 1 record added though.(which is correct).

Anyone got any inputs on this one ?

Thanks
 
news.microsoft.com said:
What if i'm doing only 1 record update at a time.

Also my insert command also has a select statement which uses the
@@Identity to get the auto # assigned to the new row.

Are you saying I update the "dsOriginal" from "dsGetchanges" and then
merge?


No, I am saying only to update original primary key value so the Merge will
match the record.
Of course, nobody prohibits you to do all merging by yourself...
 
when you say "original primary key" value you mean update the "-1" that
dataset assigns to the one that the database generates which we get by
@@Identity ?
 
Back
Top