Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Why does DataSet Merge add a new row instead of updating an existing row?

I have a DataSet that can be updated when someone updates a DataGrid.  After the update, I want to Merge the changes from the DataGrid's DataSet to another DataSet. Here is some sample code:

     DataSet ds = (DataSet)dataGrid1.DataSource;
     ds.AcceptChanges();
     DataSet dsClone = ds.Copy();
     dsClone.AcceptChanges();

     DataRow dr = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex];
     dr["subject"] = "hello";
               
     DataSet changes = ds.GetChanges();
     ds.AcceptChanges();

     if (changes != null)
     {
          dsClone.Merge(changes, false, MissingSchemaAction.Error);
          dsClone.AcceptChanges();
     }

     
After this code is executed, I end up with an extra row in the cloned DataSet and the row does not get updated.  The DataSet was created from an XML file shown below.  I did a DataSet.ReadXML and then the DataGrid's DataSource member was set to the DataSet.  The XML has a primary key (msgId).


<?xml version="1.0" standalone="yes"?>
<MessageList xmlns="http://tempuri.org/Messages.xsd">
  <Msg>
    <msgId>10</msgId>
    <descr>Yesterday</descr>
    <subject>some subject</subject>
    <msgText versionNo ="1.0">disk is full</msgText>
  </Msg>
  <Msg>
    <msgId>5</msgId>
    <descr>Message #1</descr>
    <subject>some other subject</subject>
    <msgText versionNo="1.0">The message goes here</msgText>
  </Msg>
  </MessageList>

How can I get the DataSet to UPDATE the row as opposed to adding a brand new row when I call Merge?

THANKS!
0
mromeo
Asked:
mromeo
  • 3
  • 2
1 Solution
 
rajaloysiousCommented:
You may have not set a primary key.
Try setting a primary key and check this..

cheers
0
 
rajaloysiousCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatasetclassmergetopic2.asp
says so:
When merging a new source DataSet into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted, are matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.

cheers
0
 
mromeoAuthor Commented:
My xml has a primary key.   The primary key is msgId.  If it has a primary key, shouldn't it match it up against the same record?  Do you need to see the schema?
0
 
rajaloysiousCommented:
I guess the primary keys need to be set up in your datatables (inside the dataset)

DataColumn [] primarykeys1 = new DataColumn[1];
keys1[0] = DataSet1.Tables["<table_name>"].Columns["msgId"];
DataSet1.Tables["<table_name>"].PrimaryKey = primarykeys1 ;

OR

DataColumn [] primarykeys1 = new DataColumn[1];
keys1[0] = DataSet1.Tables[0].Columns[0];
DataSet1.Tables[0].PrimaryKey = primarykeys1 ;

cheers
0
 
mromeoAuthor Commented:
Yes, that it did it!! Thanks.  You need to make sure you specify the primary key in BOTH the source and target.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now