468,257 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

"DataSet.Merge()" simple problem

Hi there,

Can anyone explain the following (very) simple scenario.

1) I make an exact copy of my "DataSet" and delete one record from a given
table (in the copy)
2) I invoke "DataSet.GetChanges()" on the above copy and pass the results to
"DataSet.Merge()" on the original copy
3) If I now inspect the original copy, it shows that the record has *not*
been deleted from the table. However, a call to "GetChanges()" does show my
deleted record.

Why doesn't the record get deleted from the original table after the merge
in step 2? Note that I've inspected my original "DataSet" after step 2 as
well as the "DataSet" returned by "DataSet.GetChanges()". Apparently
"DataSet.GetChanges()" returns a "DataSet" that's completely empty of data
except for the one affected table which contains the single deleted record.
That record is then simply added to the table in the original "DataSet" when
"DataSet.Merge()" is called. The original table therefore winds up with two
copies of the record, the origiinal and the deleted. Does anyone know what's
going on here. Thanks in advance.
May 19 '07 #1
5 16211
Hi,
I am not entirely sure if i had grasped your query correctly.I have written
a code snippet in C# Windows App and it is working perfectly well.

DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));

DataRow dr1 = dt1.NewRow();
dr1[0] = 1;
dt1.Rows.Add(dr1);

DataRow dr2 = dt1.NewRow();
dr2[0] = 2;
dt1.Rows.Add(dr2);

ds1.Tables.Add(dt1);

ds1.Tables[0].Rows.RemoveAt(0);

DataSet ds2 = new DataSet();

ds2 = ds1.GetChanges();

ds1.AcceptChanges();

ds1.Merge(ds2);

MessageBox.Show(ds1.Tables[0].Rows.Count.ToString());
please do let me know if this is what you are trying to do.
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Mark Chambers" wrote:
Hi there,

Can anyone explain the following (very) simple scenario.

1) I make an exact copy of my "DataSet" and delete one record from a given
table (in the copy)
2) I invoke "DataSet.GetChanges()" on the above copy and pass the results to
"DataSet.Merge()" on the original copy
3) If I now inspect the original copy, it shows that the record has *not*
been deleted from the table. However, a call to "GetChanges()" does show my
deleted record.

Why doesn't the record get deleted from the original table after the merge
in step 2? Note that I've inspected my original "DataSet" after step 2 as
well as the "DataSet" returned by "DataSet.GetChanges()". Apparently
"DataSet.GetChanges()" returns a "DataSet" that's completely empty of data
except for the one affected table which contains the single deleted record.
That record is then simply added to the table in the original "DataSet" when
"DataSet.Merge()" is called. The original table therefore winds up with two
copies of the record, the origiinal and the deleted. Does anyone know what's
going on here. Thanks in advance.
May 19 '07 #2
Thanks for the feedback. Try this instead:

///////////////////////////////////////////////////
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));

DataRow dr1 = dt1.NewRow();
dr1[0] = 1;
dt1.Rows.Add(dr1);

DataRow dr2 = dt1.NewRow();
dr2[0] = 2;
dt1.Rows.Add(dr2);

ds1.Tables.Add(dt1);
ds1.AcceptChanges();

DataSet ds2 = ds1.Copy();
ds2.Tables[0].Rows[0].Delete(); // Delete ID 1

DataSet ds3 = ds2.GetChanges();
ds1.Merge(ds3);
///////////////////////////////////////////////////

After the merge, "ds1" contains the original (unaltered) rows plus one extra
(deleted) row with ID 1. Shouldn't the original row with ID 1 simply be
deleted instead?
May 19 '07 #3
Mark,

I miss the primary key in your tables?

Cor

"Mark Chambers" <no_spam@_nospam.comschreef in bericht
news:OQ****************@TK2MSFTNGP03.phx.gbl...
Thanks for the feedback. Try this instead:

///////////////////////////////////////////////////
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));

DataRow dr1 = dt1.NewRow();
dr1[0] = 1;
dt1.Rows.Add(dr1);

DataRow dr2 = dt1.NewRow();
dr2[0] = 2;
dt1.Rows.Add(dr2);

ds1.Tables.Add(dt1);
ds1.AcceptChanges();

DataSet ds2 = ds1.Copy();
ds2.Tables[0].Rows[0].Delete(); // Delete ID 1

DataSet ds3 = ds2.GetChanges();
ds1.Merge(ds3);
///////////////////////////////////////////////////

After the merge, "ds1" contains the original (unaltered) rows plus one
extra (deleted) row with ID 1. Shouldn't the original row with ID 1 simply
be deleted instead?

May 19 '07 #4
I miss the primary key in your tables?

Hmmm, very strange. When I add the primary key to this example (forget it
the first time - thanks) it works fine. In my own code however the primary
keys are there but it doesn't work. This problem is almost identical to
another one I just found here (by a C# MVP):

http://forums.microsoft.com/MSDN/Sho...32312&SiteID=1

I'll have to play around with this some more to figure out what's going on.
It seems like there's a possible MSFT bug somewhere.
May 19 '07 #5
>I miss the primary key in your tables?
>
Hmmm, very strange. When I add the primary key to this example (forget it
the first time - thanks) it works fine. In my own code however the primary
keys are there but it doesn't work. This problem is almost identical to
another one I just found here (by a C# MVP):

http://forums.microsoft.com/MSDN/Sho...32312&SiteID=1

I'll have to play around with this some more to figure out what's going
on. It seems like there's a possible MSFT bug somewhere.
Ok, thanks for each of your help. It was the primary key after all. The
designer apparently reset it in one of my tables without my knowledge. None
of these keys have been touched by me in ages however but the designer isn't
always stable and does things like this from time-to-time (as an example,
the forms designer frequently sets "Forms.CancelButton" back to null for no
apparent reason). Anyway, thanks again (appreciated).
May 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nick | last post: by
1 post views Thread by newbie001 | last post: by
18 posts views Thread by Sender | last post: by
12 posts views Thread by Peter Proost | last post: by
2 posts views Thread by Simon Harvey | last post: by
8 posts views Thread by rdrink | last post: by
7 posts views Thread by axlq | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.