473,499 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (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
1 6121
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
749
by: Bruce Stockwell | last post by:
the setup: Webservice/WinClient application/SQL server. VS.Net (visual basic) winform wizard creates a simple form with load cancel cancelall and datagrid bound to a simple Dataset with one...
3
6015
by: John R. | last post by:
I have an application written in C# and i am using MS XML DOM! I have a document with the following structure (only the <DicEntry> - Elements are important): <NewDataSet...
0
1270
by: matt.schaunaman | last post by:
I am trying to merge multiple xml files into one dataset, but I keep getting "'column' argument cannot be null. Parameter name: column " any help would be appreciated! I am getting the error on...
1
4990
by: GoogleGroups | last post by:
It seems like this should be easy, but it has proven to be quite painful. I have an existing dataset, which I used the GetChanges and WriteXML functions upon to write out a DiffGram to a local...
1
15754
by: mj2736 | last post by:
I'm a little confused about DataTable.Merge(). I have two DataTable objects with the same structure - dtOrig and dtCurrent. The end result I'm trying to achieve is to get all the rows in dtCurrent...
5
16694
by: Mark Chambers | last post by:
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...
0
1279
by: buk110 | last post by:
Hi guys, I'm really hoping that someone can help me... I have 3 datasets. One that will contain the initial information when I open up the database; one that will pull in information afterwards;...
5
1836
by: Franck | last post by:
how come unchanged always true even if data changed This code come from my saving button: ============================================ DataSet ds1 = new DataSet(); DataSet ds2 = new...
3
2313
by: John Sheppard | last post by:
Hello there, I have the following code; I do this as a work around for a dataset.haschanges method which doesnt appear to work correctly either For Each dt As DataTable In myDs.Tables If Not...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.