SQLDataAdapter.Update exception reporting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use some code example from MSDN like the following:

Try
SqlDataAdapter1.Update(DataSet1User21, _
"Shippers")
PopulateGridFromDB()
Catch ex As Exception
...

But for some reason it doesn't work for me this way. Each time an error
occours on the Update, instead of an exception being caught, all I see is
that no exception was thorwn, but the DataRows in the DataSet were populated
with error messages.

Is this configurable? How do I make the system just to throw the exception
so that my code can catch?

Thanks

Feng
 
The SqlDataAdapter class has the property ContinueUpdateOnError. If this
property is set to true, no exceptions are thrown by the Update method. In
this case you should check DataSet.HasErrors

Regards from Madrid (Spain)
Jesús López
MVP
 
Just to improve your research... you can use the RowUpdated event to
catch the exceptions per datarow during the update process.

I used in a project the code is like this:

private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
if (e.Errors != null)
{
string Tablename = e.Row.Table.TableName;
ErrorList.Append("Error updating table " + Tablename + ": "+
e.Errors.Message + Environment.NewLine);
for (int Index = 0; Index <= e.Row.Table.Columns.Count + 1; Index++)
{
string ColumnName = e.Row.Table.Columns[Index].ColumnName;
string Value = e.Row[Index].ToString();
ErrorList.Append(ColumnName + " = " + Value +
Environment.NewLine);
}
}
}

ErrorList is a StringBuilder object that I have in the class to log all
exceptions. In the code above the result will be the tablename and all
it's columns and it's values. It helped me a lot in finding some bugs. I
hope it will be useful for you too.

regards from Brazil,

Andre Botelho
 
Back
Top