Microsoft KB Archive/317961

= A field that contains only NULL values is not copied into a new DataSet when you use the GetXml method to copy data from one DataSet to another DataSet =

Article ID: 317961

Article Last Modified on 3/9/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 2.0

-



This article was previously published under Q317961



SYMPTOMS
When you use the GetXml method to copy data from one DataSet object to another, if any of the fields in the original DataSet contain only NULL (DbNull) values, these fields are not copied into the new DataSet.



CAUSE
This problem occurs because the DataSet.GetXml method does not include schema information. If you use another DataSet to read this output, the DataSet depends on the InferSchema value to recover schema information. However, because all of the values in the column are NULL, the column is absent from the GetXml output, and InferSchema cannot recover this column information.

For additional information about why attributes are not generated for fields that contain a NULL value, click the article number below to view the article in the Microsoft Knowledge Base:

296393 PRB: Attributes Are Not Generated for Fields That Contain a NULL Value When ADO Recordset Is Persisted in XML



RESOLUTION
To resolve this problem, use the Copy method instead of the GetXml method. Microsoft recommends that you use the Copy method because the Copy method does not convert the DataSet to Extensible Markup Language (XML) and back.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. Create a new Windows application in Visual Basic .NET. Make sure that your project contains a reference to the System.Data namespace; add a reference to this namespace if it does not. Drag a Button control and two DataGrid controls from the toolbox to Form1. Change the Name property of the Button control to btnTest, and then change the Text property of the Button control to Test.</li>  Use the Imports statement on the System.Data, the System.Data.SqlClient, and the System.IO namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the &quot;General Declarations&quot; section of Form1: Imports System.Data Imports System.Data.SqlClient Imports System.IO                   </li>  Add the following code in the btnTest_Click event: Dim myConnString As String = _ &quot;User ID=myUID;password=myPWD;Initial Catalog=Northwind;Data Source=mySQLServer&quot; Dim mySelectQuery As String = &quot;SELECT * FROM Customers WHERE Region IS NULL&quot; Dim con As New SqlConnection(myConnString) Dim daCust As New SqlDataAdapter(mySelectQuery, con) Dim ds As New DataSet

daCust.Fill(ds, &quot;Cust&quot;) 'Fill the initial DataSet. DataGrid1.DataSource = ds        DataGrid1.DataMember = &quot;Cust&quot; Dim ds1 As New DataSet

Dim sr As New StringReader(ds.GetXml) ds1.ReadXml(a) 'Comment out the previous two lines and uncomment the next line 'to display columns with all NULL values. 'ds1 = ds.Copy

DataGrid2.DataSource = ds1 DataGrid2.DataMember = &quot;Cust&quot; </li> Modify the connection strings as appropriate for your environment.</li> Save your project. Click Start on the Debug menu to run your project.</li>  Click Test. Notice that the Region column appears in the first data grid and not in the second.

Resolution
</li>  Comment out the lines of code that use the GetXml method as follows: 'Dim sr As New StringReader(ds.GetXml) 'ds1.ReadXml(a) </li>  Uncomment the line of code that uses the Copy method as follows: ds1 = ds.Copy </li> Save your project. Click Start on the Debug menu to run your project again.</li> Click Test. Notice that the Region column appears in both of the data grids.</li></ol>

<div class="references_section">