DOC: Error in SqlCommandBuilder Code Sample (316758)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 1.1)

This article was previously published under Q316758
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data.SqlClient

SUMMARY

When you execute the code in the MSDN documentation for the SqlCommandBuilder class, you may receive the following exception:
An unhandled exception of type 'System.Data.MissingPrimaryKeyException' occurred in system.data.dll

Additional information: Table doesn't have a primary key.

MORE INFORMATION

To obtain additional information about the DataSet object, such as the primary key information, 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 the MissingSchemaAction property, click the article number below to view the article in the Microsoft Knowledge Base:

310128 INFO: When to Use FillSchema and MissingSchemaAction with the DataAdapter in ADO.NET

Code Sample for Visual Basic .NET

Public Function SelectSqlSrvRows(myDataSet As DataSet, myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
    Dim myConn As New SqlConnection(myConnection)
    Dim myDataAdapter As New SqlDataAdapter()
    myDataAdapter.SelectCommand = New SqlCommand(mySelectQuery, myConn)
    Dim custCB As SqlCommandBuilder = New SqlCommandBuilder(myDataAdapter)

    myConn.Open()
    Dim custDS As DataSet = New DataSet

    'Use one of the following two methods to fix the problem.
    'Using MissingSchemaAction 
    myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    'Using FillSchema
    'custDS.FillSchema(ds, SchemaType.Source, "Customers") 

    myDataAdapter.Fill(custDS, "Customers")

    ' Code to modify data in DataSet here 

    ' Without the SqlCommandBuilder this line would fail.
    myDataAdapter.Update(custDS, "Customers")

    myConn.Close()
End Function 'SelectSqlSrvRows
				

Code Sample for C# .NET

public DataSet SelectSqlSrvRows(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName) {
    SqlConnection myConn = new SqlConnection(myConnection);
    SqlDataAdapter myDataAdapter = new SqlDataAdapter();
    myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
    SqlCommandBuilder custCB = new SqlCommandBuilder(myDataAdapter);

    myConn.Open();

    DataSet custDS = new DataSet();
    
    //Use one of the following two methods to fix the problem.
    //Using MissingSchemaAction 
    myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    //Using FillSchema
    //custDS.FillSchema(ds, SchemaType.Source, "Customers");

    myDataAdapter.Fill(custDS, "Customers");

    //Code to modify data in DataSet here

    //Without the SqlCommandBuilder this line would fail
    myDataAdapter.Update(custDS, "Customers");

    myConn.Close();

    return custDS;
 }
				

Modification Type:MinorLast Reviewed:4/24/2003
Keywords:kbbug kbdocerr kbnofix kbSqlClient kbSystemData KB316758