views:

521

answers:

1

I am trying to get all the rows that exist in allData but not in removeData

public static DataTable RemoveDuplicateRows(DataTable allData, 
    DataTable removeData) 
{
    removeData.Merge(allData);
    DataTable newData = removeData.GetChanges(); 
    removeData.RejectChanges();
    return newData;
}

removeData is empty prior to the call in this case (just a new DataTable();)

But newData always has a value of null after the DataTable newData = removeData.GetChanges(); line

Final Solution:

 public static DataTable RemoveDuplicateRows(DataTable allData, DataTable removeData) 
 {
  DataTable duplicate = allData.Clone();
  foreach (DataRow row in allData.Rows)
  {
   duplicate.ImportRow(row);
  }
  foreach (DataRow row in duplicate.Rows)
  {
   row.SetAdded();
  } 

  removeData.Merge(duplicate);
  DataTable newData = removeData.GetChanges(DataRowState.Added);
  removeData.RejectChanges();
  allData.RejectChanges();
  return newData;
 }
A: 

Your removeData DataTable needs to have the same columns/fields as allData. In other words, it can't just be a new DataTable().

MusiGenesis
So, now I'm writing the old data and schema to the filesystem (WriteXml and WriteXmlSchema), I then load them into removeData (with ReadXmlSchema and ReadXml). And allData contains the data from the db (which has 2 more records than what is in the filesystem xml). So when I get to this code I see that allData has 21 rows and removeData has 19. The merge then causes removeData to have 40! And then GetChanges is still returning null
Chad
Fixed the primary keys so that when I merge they both have 21 rows, but GetChanges still returns NULL!
Chad
Had to work some mojo to get the added rows to be marked as added, I updated the original question with the final solution
Chad