[ Team LiB ] |
Recipe 2.15 Retrieving Update ErrorsProblemYou want to access all of the error information available after an update fails. SolutionUse one of the available properties (such as HasErrors) and methods (such as GetErrors( )) to obtain the error information. The schema of table TBL0215 used in this solution is shown in Table 2-14.
The sample code contains two event handlers:
The C# code is shown in Example 2-20. Example 2-20. File: RetrieveProviderErrorsForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Text; using System.Data; using System.Data.SqlClient; private const String TABLENAME = "TBL0215"; private DataSet ds; private SqlDataAdapter da; // . . . private void RetrieveProviderErrorsForm_Load(object sender, System.EventArgs e) { String sqlText = "SELECT * FROM " + TABLENAME; // Create the DataAdapter and its CommandBuilder. da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.ContinueUpdateOnError = true; SqlCommandBuilder cb = new SqlCommandBuilder(da); // Create a table and fill it. DataTable dt = new DataTable(TABLENAME); dt.Columns.Add("UniqueId", typeof(Int32)); dt.Columns.Add("NumericField"); // Data type not specified dt.Columns.Add("StringNoNullsField", typeof(String)); dt.Columns.Add("ConstrainedNegativeField", typeof(Int32)); da.Fill(dt); // Create the DataSet and add the table. ds = new DataSet( ); ds.Tables.Add(dt); // Bind the default view for the table to the grid. dataGrid.DataSource = dt.DefaultView; } private void updateButton_Click(object sender, System.EventArgs e) { StringBuilder result = new StringBuilder( ); if (testColumnErrorCheckBox.Checked) // Set a column error on to demonstrate. ds.Tables[0].Rows[0].SetColumnError(0, "test error"); else // Clear the demonstration column error. ds.Tables[0].Rows[0].SetColumnError(0, ""); da.Update(ds, TABLENAME); result.Append("Dataset.HasErrors = " + ds.HasErrors + Environment.NewLine); // Iterate over the collection of tables in the DataSet. foreach(DataTable dt in ds.Tables) { // Display whether the table has errors. result.Append("\tTable [" + dt.TableName + "] HasErrors = " + dt.HasErrors + Environment.NewLine); int rowCount = 0; // Iterate over the rows in the table having errors. foreach(DataRow row in dt.GetErrors( )) { // Display whether error information for the row. result.Append("\t\tRow [" + (++rowCount) + "] HasErrors = " + row.HasErrors + Environment.NewLine); if(row.RowError != "") result.Append("\t\t" + row.RowError + Environment.NewLine); // Iterate over the column errors for the row. foreach(DataColumn col in row.GetColumnsInError( )) { // Display error information for the column. result.Append("\t\t\tColumn [" + col.ColumnName + "]: " + row.GetColumnError(col) + Environment.NewLine); } } } resultTextBox.Text = result.ToString( ); } DiscussionThe Update( ) method of the DataAdapter is used to reconcile changes made to a DataSet back to the underlying data source. If errors occur during the reconciliation, the ContinueUpdateOnError property of the DataAdapter specifies whether the update continues with remaining rows or stops processing:
Once the update has completed, there are a number of properties and methods you can use to investigate errors in the DataSet, DataTable, DataRow, and DataColumn objects:
The solution code demonstrates how to use these error checking methods and properties when updates are made with user-specified data. |
[ Team LiB ] |