"DataSet.Merge()" simple problem

  • Thread starter Thread starter Mark Chambers
  • Start date Start date
M

Mark Chambers

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.
 
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 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?
 
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/ShowPost.aspx?PostID=632312&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.
 
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/ShowPost.aspx?PostID=632312&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).
 
Back
Top