By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Problem with Dataset Merge and preserveChanges = TRUE

P: n/a
Hello all,
Here is my problem:
I am trying to merge 2 datasets but I don't want to overwrite rows
that are already modified in my working dataset.

Example:
I have one Dataset with only one DataTable in it. The DataTable has
these 2 columns:

Column #1: Name="Id" Type=Int32 (Primary Key)
Column #2: Name="Value" Type=String

My first DataSet (datasetOriginal) has these values in its DataTable:

| Id | Value | RowState |
1 "test1" Unchanged
2 "test2" Modified

My second Dataset (datasetNew) has these values in its DataTable:
| Id | Value | RowState |
1 "newValue" Modified

Then, I merge the datasetNew into the datasetOriginal with this call:
"datasetOriginal.Merge( datasetNew, true );". I set
preserveChanges=true to avoid overwriting the modifications in the
datasetOriginal.

I was thinking that by doing so, it would give me a "datasetOriginal"
with these values in its DataTable:

| Id | Value | RowState |
1 "newValue" Modified
2 "test2" Modified

But what I have is this:
| Id | Value | RowState |
1 "test1" Modified
2 "test2" Modified

The Value is never modified. If I set the "preserveChanges=false" in
the Merge function, then the value is correctly merged.

I have experimented by adding or removing the call to AcceptChanges()
before merging my DataSets, but it doesn't help.

Is there something special I need to do to make the Merge() work or is
there something I don't understand correctly with the behaviour of the
Merge() function?

Thanks in advance,
Jonathan

Jul 30 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I don't seem to find an answer to my question, so I have decided to do
the merge manually in the case of preserveChanges=true.

The behaviour we want is to merge every row from an incoming dataset
in the original dataset but without modifying row that are already
modified in the original dataset.

If it can be any help, here is the code that does this:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void Merge(DataSet dsOriginal, DataSet dsNew, bool
preserveChanges)
{
if (preserveChanges == false)
{
dsOriginal.Merge(dsNew);
}
else
{
foreach (DataTable dt in dsNew.Tables)
{
// do we have something to do?
if ( dt.Rows.Count 0 )
{
DataColumn[] ardc = dt.PrimaryKey;
object[] arPrimaryKey = new object[ardc.Length];
foreach (DataRow dr in dt.Rows)
{
// compute the primary key
for (int i = 0; i < arPrimaryKey.Length; ++i)
{
arPrimaryKey[i] = dr[ardc[i]];
}
DataRow drOriginal =
dsOriginal.Tables[dt.TableName].Rows.Find(arPrimaryKey);

if ((drOriginal != null) && (drOriginal.RowState !
= DataRowState.Unchanged))
{
// don't merge this row
dr.Delete();
dr.AcceptChanges();
}
}
// merge the remaining rows
dsOriginal.Tables[dt.TableName].Merge(dt);
}
}
}
return;
}
Jul 31 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.