DataAdapter Events

The FillError event is most commonly raised when the data being added violates a constraint in the DataSet or when the data being added can’t be converted to a .NET Framework data type without a loss of precision. When a FillError event occurs, the current row isn’t added to the DataTable. Handling the FillError event allows the error to be resolved and the row to be either added or ignored before resuming the Fill( ) operation with the next row.

The FillError event handler receives an argument of FillErrorEventArgs, which contains specific data about the event that can effectively respond to and handle the error. The Continue property of the FillErrorEventArgs argument determines whether an exception is thrown or processing continues because of the error.

The following example demonstrates handling the FillError event when filling a table containing three columns:

SqlDataAdapter da;



// ... code to set up the data adapter



da.FillError += new FillErrorEventHandler(da_FillError);

DataSet ds = new DataSet();

da.Fill(ds, "MyTable");



private void da_FillError(object sender, FillErrorEventArgs e)

{

    // ... code to identify and correct the error



    // add the fixed row to the table

    DataRow dr = e.DataTable.Rows.Add(new object[] {e.Values[0],

        e.Values[1], e.Values[2]});

    

    // continue the Fill with the rows remaining in the data source

    e.Continue = true;

}

The RowUpdating event is raised before changes to a row are submitted to the data source. The RowUpdating event handler can ...

Get ADO.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.