Problem with Dataset Merge and preserveChanges = TRUE

J

joproulx

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
 
J

joproulx

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 = dr[ardc];
}
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top