Problem with Dataset Merge and preserveChanges = TRUE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joproulx@hotmail.com

    Problem with Dataset Merge and preserveChanges = TRUE

    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 (datasetOrigina l) 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:
    "datasetOrigina l.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 "datasetOrigina l"
    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 "preserveChange s=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

  • joproulx@hotmail.com

    #2
    Re: Problem with Dataset Merge and preserveChanges = TRUE

    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 (preserveChange s == false)
    {
    dsOriginal.Merg e(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.Le ngth; ++i)
    {
    arPrimaryKey[i] = dr[ardc[i]];
    }
    DataRow drOriginal =
    dsOriginal.Tabl es[dt.TableName].Rows.Find(arPr imaryKey);

    if ((drOriginal != null) && (drOriginal.Row State !
    = DataRowState.Un changed))
    {
    // don't merge this row
    dr.Delete();
    dr.AcceptChange s();
    }
    }
    // merge the remaining rows
    dsOriginal.Tabl es[dt.TableName].Merge(dt);
    }
    }
    }
    return;
    }

    Comment

    Working...