473,772 Members | 2,448 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 (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

Jul 30 '08 #1
1 6197
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;
}
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 Datatable. No coding by programer, All wizardry. User starts app, opens form, adds (4) records and clicks update.
3
6063
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance">
0
1289
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 ds1.Merge(...) Dim ds1 as New DataSet Dim bFirstNewsFeed As Boolean = True
1
5022
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 XML file. I want to use this same dataset (in another instance of the application) and the DiffGram to display the changes in the DataGridView as they existed after the user made his/her change. In English:
1
15786
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 that are in some way different from the corresponding rows in dtOrig so those new/ modified/deleted rows can be processed. My idea was to use dtOrig.Merge(dtCurrent), and then call dtOrig.GetChanges(). But what I found is that when the...
5
16705
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 "DataSet.GetChanges()" on the above copy and pass the results to "DataSet.Merge()" on the original copy 3) If I now inspect the original copy, it shows that the record has *not* been deleted from the table. However, a call to "GetChanges()" does show my
0
1300
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; and a 3rd to show the changes that have occured since merging #1 and #2. Now when I'm testing this data, it seems that HasChanges always returns true. Am I invoking something wrong? Am I not using AcceptChanges and HasChanges properly?? ...
5
1862
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 DataSet(); DataSet ds3 = new DataSet(); //Static Dataset which contain values when my form load
3
2342
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 dt.GetChanges(DataRowState.Added) Is Nothing Then hasChanges=true If Not dt.GetChanges(DataRowState.Modified) Is Nothing Then hasChanges=true If Not dt.GetChanges(DataRowState.Deleted) Is Nothing Then
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6713
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.