Microsoft KB Archive/308908

= HOW TO: Get Extended Error Information for the DataSet in Visuall C++ .NET =

Article ID: 308908

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1

-



This article was previously published under Q308908



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

IN THIS TASK
SUMMARY
 * Requirements
 * Get Extended Error Information
 * Build the Sample Code

REFERENCES



SUMMARY
The DataAdapter object throws generic exceptions when problems occur.

Use this step-by-step guide to check for and report errors for each row and column in each table in a DataSet. You can use this information in update scenarios where it is important to check for errors in any row or column.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows XP Professional, Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET
 * ADO.NET fundamentals and syntax

back to the top

Get Extended Error Information
To get extended error information in a typical update scenario, follow these steps:
 * 1) Use the HasChanges method to detect any additions, modifications, or deletions in any new rows in the DataSet.

-or-

Create a subset of the original DataSet and then use the GetChanges method to check for any errors.

NOTE: The subset should contain only new, modified, or deleted rows.
 * 1) Use the HasErrors property to verify if there are any errors in any of the tables in the DataSet.
 * 2) If no errors are found in the DataSet, continue with the update operation.
 * 3) If errors are found in the DataSet, use the GetErrors method to identify the rows that contain the errors. In the sample shown below, all tables in the DataSet are checked for errors. However, you can check one or more specific tables in the DataSet.
 * 4) After you identify the rows that contain errors, use the GetColumnError method to determine the columns in fault for each row with errors.

back to the top

Build the Sample Code
The following sample code uses the Customers table in the Northwind database that is included with Microsoft SQL Server.  In Visual Studio .NET, create a new Managed C++ Application.  Replace the default code in your application's source file (your_name.cpp) with the following code:
 * 1) include &quot;stdafx.h&quot;


 * 1) using 
 * 2) using 
 * 3) using 
 * 4) using 

using namespace System; using namespace System::Data; using namespace System::Data::SqlClient;

// This is the entry point for this application. int wmain(void) int main(void) { DataSet *myDataSet = new DataSet; DataSet *newDataSet; DataRow *rowsInError[];
 * 1) ifdef _UNICODE
 * 1) else
 * 1) endif

SqlConnection *myCn = new SqlConnection; myCn->ConnectionString = &quot;Server=your_sql_server;User ID=your_user_id;Password=your_password;Initial Catalog=Northwind;&quot;; SqlDataAdapter *myDACust = new SqlDataAdapter(&quot;Select * From Customers&quot;, myCn); SqlCommandBuilder *myCmdBlder = new SqlCommandBuilder(myDACust);

try {   DataRow *myRow; DataTable *myTable; myCn->Open; //get schema information for the Update myDACust->MissingSchemaAction = MissingSchemaAction::AddWithKey; myDACust->Fill(myDataSet, &quot;Customers&quot;);

myTable = myDataSet->Tables->Item[&quot;Customers&quot;]; myRow = myTable->Rows->Item[0]; String *strValue = &quot;Jefferson&quot;; //Change the ContactName to Jefferson. myRow->set_Item(&quot;ContactName&quot;, strValue ); if (myDataSet->HasChanges) {       //Create a smaller DataSet containing only modified rows. newDataSet = myDataSet->GetChanges; if(!newDataSet->HasErrors) {        // If no errors, update the Customers table. myDACust->Update(myDataSet, &quot;Customers&quot;); Console::WriteLine(&quot;Update was processed successfully&quot;); }   else {      DataTable *newTable; // Check each table's HasErrors property. for (int i=0; i < newDataSet->Tables->Count-1; i++) // newDataSet->Tables ; {         newTable = newDataSet->Tables->Item[i];

// If HasErrors is true, reconcile errors. if(newTable->HasErrors) {       // Use GetError to get an array of all rows with errors. rowsInError = newTable->GetErrors;

// Print the error of each column in each row. for(int i = 0; i < rowsInError->Length; i++) {          DataColumn *newCol; for (int j=0; j< newTable->Columns->Count-1;j++) //while (newCol newTable->Columns) {            newCol = newTable->Columns->Item[j]; Console::WriteLine(&quot; {0} column has the following error: {1} &quot;,            newCol->ColumnName, rowsInError[i]->GetColumnError(newCol)); }           // Clear the row errors. rowsInError[i]->ClearErrors; }         }       }         }     }   }  catch(Exception *e) {   Console::WriteLine(e->Message->ToString); } __finally {   // Close Connection to SQL myCn->Close; }

Console::ReadLine; return 0; }                    Modify the parameters of the ConnectionString property of the SqlConnection object as appropriate to connect to your SQL Server properly. On the Debug menu in Visual Studio .NET Integrated Development Edition (IDE), click Run Without Debugging to execute the code and open a Console window.

If no errors occurred during the update, the Console window displays the following message:

Update was processed successfully.

If errors occurred during the update, the Console window displays the errors and tells you in which columns the errors occurred. Press ENTER to close the Console window and stop the application.</li></ol>

back to the top

<div class="references_section">