FIX: DataSet does not load the original records of DiffGram correctly (319919)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q319919

SYMPTOMS

Under the following circumstances
  • A modified DataSet is persisted to Extensible Markup Language (XML) as a DiffGram. -and-

  • You bind a DataGrid to the DataView object that is retrieved from the DataTable object. -and-

  • You set the DataView.RowStateFilter property to ModifiedOriginal.
you receive the following error message:
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an object.
The same behavior occurs when you use the Select method of a DataTable object if you set the DataViewRowState property to ModifiedCurrent, OriginalRows, or Deleted.

CAUSE

When the original records in the DiffGram are loaded into the DataSet, correct indexes for the row versions are not created. As a result, you cannot use any original records in indexing operations.

When you set the RowStateFilter property, the Error property of the DataRowView is accessed. This property accesses the "row" member variable, which is set to null. As a result, you receive a NullReferenceException exception.

RESOLUTION

To work around this problem, call the DataSet.Copy method to copy the DataSet that is loaded from the XML DiffGram. You can then successfully set the DataViewRowState of the new DataSet to ModifiedOriginal. For more information about how to do this, see the steps in the "More Information" section.

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 Visual Studio .NET (2003), Professional Edition.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Open Visual Studio .NET, and then create a new Microsoft Visual Basic .NET Windows Application project named DiffGramTest.
  2. Add the following statement to the top of Form1.vb:
    Imports System.Data.SqlClient
    					
  3. Drag a DataGrid control from the Windows Forms toolbox onto Form1.
  4. Double-click Form1 to create a Form1_Load event handler, and then add the following code to the handler:
    Dim da As New SqlDataAdapter("select * from customers", "server=myserver;" & _
                                 "uid=myuser;pwd=mypassword;database=northwind")
    Dim ds As New DataSet()
    
    'Fill DataSet with data from table.
    da.Fill(ds)
    'Modify first row of table.
    ds.Tables(0).Rows(0).BeginEdit()
    ds.Tables(0).Rows(0)("customerid") = "AAKK"
    ds.Tables(0).Rows(0).EndEdit()
    
    'Write the modified table to an XML file as a DiffGram.
    ds.WriteXmlSchema("c:\diffschema.xml")
    ds.WriteXml("c:\difftest.xml", XmlWriteMode.DiffGram)
    
    'Create second DataSet to read XML DiffGram.
    Dim ds2 As New DataSet()
    ds2.ReadXmlSchema("c:\diffschema.xml")
    ds2.ReadXml("c:\difftest.xml", XmlReadMode.DiffGram)
    
    'Bind DataGrid to DataView that is retrieved from DataTable.
    Dim dv As DataView = ds2.Tables(0).DefaultView
    DataGrid1.DataSource = dv
    
    'Set the RowStateFilter of DataView to ModifiedOriginal.
    dv.RowStateFilter = DataViewRowState.ModifiedOriginal
    					
  5. Modify the connection string of the SqlDataAdapter object to connect to your instance of Microsoft SQL Server. Although this sample uses the Northwind sample database that is installed with SQL Server, you can modify this code to work with any database.
  6. Press F5 to compile and to execute the code. Notice that you receive the error message that is listed in the "Symptoms" section.

    Workaround

  7. Locate the following line of code:
    Dim dv As DataView = ds2.Tables(0).DefaultView
    						
    Replace this code with the following code:
    Dim ds3 As DataSet = ds2.Copy
    Dim dv As DataView = ds3.Tables(0).DefaultView
    					
  8. Press F5 to compile and to execute the code. Notice that the exception no longer occurs.

REFERENCES

For additional information about XML integration with ADO.NET, click the article number below to view the article in the Microsoft Knowledge Base:

313649 INFO: Roadmap for XML Integration with ADO.NET

For additional information about XML schemas in the .NET Framework, click the article number below to view the article in the Microsoft Knowledge Base:

313826 INFO: Roadmap for XML Schemas in the .NET Framework


Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbfix kbbug kbpending KB319919