Microsoft KB Archive/320849

= FIX: Add Method of the DataRowCollection Does Not Reflect Changes in XmlDataDocument View =

Article ID: 320849

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q320849



SYMPTOMS
The &quot;Synchronizing a DataSet with an XmlDataDocument&quot; Microsoft Developer Network (MSDN) topic has the following information:

The .NET Framework enables real-time, synchronous access to both the relational and hierarchical representations of data through the DataSet object and the XmlDataDocument object, respectively.

When a DataSet is synchronized with an XmlDataDocument, both objects are working with a single set of data.

However, if new rows are added to the DataSet by using an object array, the DataSet and XmlDataDocument objects are not synchronized. The new rows cannot be seen in the XMLDataDocument object.



RESOLUTION
Use the DataRow object instead of an object array to add new rows to the DataTable. This produces the expected behavior and both DataSet and XMLDataDocument objects are synchronized as expected.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft ADO.NET (included with the .NET Framework) 1.1, and Microsoft .NET Framework Class Libraries 1.1.



MORE INFORMATION
The Add method of the DataRowCollection object has two overload versions:   The following adds the specified DataRow to the DataRowCollection object: Overloads Public Sub Add(DataRow)   The following creates a row by using specified values and adds it to the DataRowCollection. The values are passed as an object array: Overloads Overridable Public Function Add(Object) As DataRow 

Use the first overload version, where a DataRow object is used. This produces the expected behavior (that is, the DataSet and XMLDataDocument objects are synchronized).

Steps to Reproduce the Behavior
 Open a new Microsoft Visual Basic .NET Windows Application project. By default, Form1 is added to the application.  Import the following namespace: imports System.XML   Paste the following code in the Form1 Load event: 'Create a dataset. Dim ds As New DataSet(&quot;simpleexample&quot;)

'Add a single columned table to it. ds.Tables.Add(&quot;table1&quot;) Dim c As New DataColumn(&quot;col1&quot;) c.DataType = GetType(System.String) ds.Tables(&quot;table1&quot;).Columns.Add

'Create an xmldatadocument synchronized with the dataset. Dim xd As New XmlDataDocument(ds)

'Add a record to the table using an object array. Dim vals(0) As Object vals(0) = &quot;Add This Column&quot; ds.Tables(&quot;table1&quot;).Rows.Add(vals)

'Add a record to the table using DataRow. 'Dim dr As DataRow 'dr = ds.Tables(&quot;table1&quot;).NewRow 'dr(0) = &quot;Add This Column&quot; 'ds.Tables(&quot;table1&quot;).Rows.Add(dr)

ds.AcceptChanges MsgBox(xd.OuterXml,, &quot;Outer XML of XMLDataDocument&quot;) MsgBox(ds.Tables(&quot;Table1&quot;).Rows.Count,, &quot;Number of Rows In DataSet&quot;)

'Save the dataset and xmldatadocument and still do not see the added row in XMLDataDocument. xd.Save(&quot;firstdatadocument.xml&quot;)

' The added row does show up when doing WriteXml of Dataset. ds.WriteXml(&quot;simpleexample.xml&quot;)

</li> Run the application. The first message box shows the OuterXML of XMLDataDocument, which does not contain the newly-added row. The second message box shows the number of rows in the DataTable in DataSet, which in this case is 1.

</li> Comment the lines that use Object Array to add rows, and then uncomment the lines that use DataRow to add a new row. Run the application again. This shows the correct behavior. The first message box shows the newly-added row, and the second message box shows the number of rows in the DataTable, which in this case is 1.</li></ol>

<div class="references_section">