Microsoft KB Archive/319919

= FIX: DataSet does not load the original records of DiffGram correctly =

Article ID: 319919

Article Last Modified on 9/15/2005

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * 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 &quot;row&quot; 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 &quot;More Information&quot; 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.



Steps to Reproduce the Behavior
 Open Visual Studio .NET, and then create a new Microsoft Visual Basic .NET Windows Application project named DiffGramTest.  Add the following statement to the top of Form1.vb: Imports System.Data.SqlClient  Drag a DataGrid control from the Windows Forms toolbox onto Form1.  Double-click Form1 to create a Form1_Load event handler, and then add the following code to the handler: Dim da As New SqlDataAdapter(&quot;select * from customers&quot;, &quot;server=myserver;&quot; & _                            &quot;uid=myuser;pwd=mypassword;database=northwind&quot;) 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)(&quot;customerid&quot;) = &quot;AAKK&quot; ds.Tables(0).Rows(0).EndEdit

'Write the modified table to an XML file as a DiffGram. ds.WriteXmlSchema(&quot;c:\diffschema.xml&quot;) ds.WriteXml(&quot;c:\difftest.xml&quot;, XmlWriteMode.DiffGram)

'Create second DataSet to read XML DiffGram. Dim ds2 As New DataSet ds2.ReadXmlSchema(&quot;c:\diffschema.xml&quot;) ds2.ReadXml(&quot;c:\difftest.xml&quot;, 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  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.</li>  Press F5 to compile and to execute the code. Notice that you receive the error message that is listed in the &quot;Symptoms&quot; section.

Workaround
</li>  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 </li> Press F5 to compile and to execute the code. Notice that the exception no longer occurs.</li></ol>

<div class="references_section">