Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

Problem with Dataset Merge and preserveChanges = TRUE

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joproulx@hotmail.com - 30 Jul 2008 22:28 GMT
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
joproulx@hotmail.com - 31 Jul 2008 15:00 GMT
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;
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.