Microsoft KB Archive/310373

= The DataAdapter.Fill method does not set all of the properties of the DataTable and DataColumn objects =

Article ID: 310373

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q310373



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data



SYMPTOMS
After you use the DataAdapter.Fill method, various properties of the DataTable and DataColumn objects (such as primary keys, auto increment fields, nullable fields, unique indexes, and so on) are not set.



CAUSE
The DataAdapter object is optimized for read-only scenarios by default. The Fill method only retrieves the amount of schema that is necessary to display the data. You must take extra steps to obtain the additional schema that are necessary to update or validate an object.



RESOLUTION
To obtain additional information about the DataSet object, use one of the following methods:
 * Call the DataAdapter.FillSchema method to obtain extended schema information.
 * Set DataAdapter.MissingSchemaAction to MissingSchemaAction.AddWithKey before you call the Fill method.

For additional information about when to use the FillSchema method and MissingSchemaAction property, click the article number below to view the article in the Microsoft Knowledge Base:

310128 When to use FillSchema and MissingSchemaAction



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default. Make sure that your project contains a reference to the System.Data namespace. Place a Button control and a DataGrid control on Form1. Change the Name property of the button to btnTest and the Text property to Test .</li>  Use the Imports statement on the System and System.Data 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 Imports System.Data.OleDb Imports System.Data.SqlClient </li>  Add the following code in the Code window after the &quot;Windows Form Designer generated code&quot; region: Private Sub btnTest_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnTest.Click Dim myConnString As String = _ &quot;User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=myServer&quot; Dim ds As New DataSet Dim con As New SqlConnection(myConnString) Dim daCust As New SqlDataAdapter(&quot;Select * From Customers&quot;, con) 'Uncomment either of the following two lines to obtain additional schema information. 'daCust.MissingSchemaAction = MissingSchemaAction.AddWithKey 'daCust.FillSchema(ds, SchemaType.Source, &quot;Cust&quot;) daCust.Fill(ds, &quot;Cust&quot;)

DataGrid1.DataSource = ds       DataGrid1.DataMember = &quot;Cust&quot;

Dim colArr As DataColumn colArr = ds.Tables(0).PrimaryKey If colArr.Length.ToString = 0 Then MessageBox.Show(&quot;No Primary Key is defined.&quot;) End If

Dim i As Integer For i = 0 To colArr.GetUpperBound(0) MessageBox.Show(&quot;Primary Key : &quot; & colArr(i).ColumnName) Next i   End Sub </li> Modify the connection string (myConnString) as appropriate for your environment.</li> Save your project. On the Debug menu, click Start to run your project.</li> Click Test. Notice that you cannot retrieve the primary key information from the table.</li> Uncomment the code to either set MissingSchemaAction or use FillSchema, and then click Test again. Notice that the primary key information is retrieved properly.</li></ol>

<div class="references_section">