FIX: Add Method of the DataRowCollection Does Not Reflect Changes in XmlDataDocument View (320849)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q320849

SYMPTOMS

The "Synchronizing a DataSet with an XmlDataDocument" 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

  1. Open a new Microsoft Visual Basic .NET Windows Application project. By default, Form1 is added to the application.
  2. Import the following namespace:
    imports System.XML
    					
  3. Paste the following code in the Form1 Load event:
            'Create a dataset. 
            Dim ds As New DataSet("simpleexample")
    
            'Add a single columned table to it. 
            ds.Tables.Add("table1")
            Dim c As New DataColumn("col1")
            c.DataType = GetType(System.String)
            ds.Tables("table1").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) = "Add This Column"
            ds.Tables("table1").Rows.Add(vals)
    
    
            'Add a record to the table using DataRow. 
            'Dim dr As DataRow
            'dr = ds.Tables("table1").NewRow
            'dr(0) = "Add This Column"
            'ds.Tables("table1").Rows.Add(dr)
    
            ds.AcceptChanges()
            MsgBox(xd.OuterXml, , "Outer XML of XMLDataDocument")
            MsgBox(ds.Tables("Table1").Rows.Count, , "Number of Rows In DataSet")
    
            'Save the dataset and xmldatadocument and still do not see the added row in XMLDataDocument. 
            xd.Save("firstdatadocument.xml")
    
            ' The added row does show up when doing WriteXml of Dataset. 
            ds.WriteXml("simpleexample.xml")
    
    					
  4. 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.

  5. 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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:4/29/2003
Keywords:kbfix kbbug kbpending KB320849