Merge Error After ReadXml

  • Thread starter Thread starter Paul Wheeler
  • Start date Start date
P

Paul Wheeler

I'm having trouble with Merge creating duplicate records, violating primary
key constraints.

I have a DataSet w/ Primary keys set up for each table.
I make some changes.
Merge data from server w/ PreserveChanges = True works fine.
I make some more changes
I write it and its schema out to Xml using DiffGram mode
I restart and read the dataset and it's schema from Xml
Merge data from server w/ PreserveChanges = True or False don't work
The merge adds duplicates of the changed fields, one with the changes,
one without

The problem only occurs when there are outstanding changes when the file is
written to Xml, which makes me think that the Data->Xml->Data conversion is
changing something in the DataSet, but I can't figure out what it is.

Here is a quick sample that exemplifies the problem:

Dim connectstring As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft
Office\Office10\Samples\Northwind.mdb;"
Dim selectstring As String = "SELECT * FROM Products"
Dim tablename As String = "Products"
Dim primarykey As String = "ProductID"
Dim xmlfile As String = "data.xml"
Dim xsdfile As String = "data.xsd"

'Load The Database
Dim ds As New DataSet(tablename + "DataSet")
Dim da As New OleDbDataAdapter(selectstring, connectstring)
da.TableMappings.Add("Table", tablename)
da.Fill(ds)
ds.Tables(tablename).PrimaryKey = New DataColumn()
{ds.Tables(tablename).Columns(primarykey)}

'Make A Change
ds.Tables(tablename).Rows(5)("ProductName") = "Something Different"

'Write To XML
ds.WriteXmlSchema(xsdfile)
ds.WriteXml(xmlfile, XmlWriteMode.DiffGram)

'Read From XML
ds = New DataSet(tablename + "DataSet")
ds.ReadXmlSchema(xsdfile)
ds.ReadXml(xmlfile, XmlReadMode.DiffGram)

'Merge from source
Dim tempds As New DataSet(tablename + "Update")
da.Fill(tempds)
Try
ds.Merge(tempds, True)
'This Causes a Constraint Exception becuase duplicate rows are
created
Catch
End Try

Thanks Much,
-Paul Wheeler
 
I figured out the problem, and a workaround, just incase anyone is
interested. Basicly there is a bug in the ReadXml method that causes
modified rows to have incorrect Guids, so the row matching in a Merge or
Fill doesn't work properly. The work around is simply to make a copy using
DataSet.Copy(). The copy will be correct and work with the Merge or Fill
functions.
More info is available here:
http://www.daveandal.com/alshed/datasetkludges/default.asp

If anybody knows of a way to fix the problem without creating a new DataSet
please let me know. The problem with the current workaround is that if you
replace the dataset with a copy any bindings to the old dataset are lost.

-Paul Wheeler
 
Back
Top