Exploring the DataAdapter and DataTable Events
You'll find all the code examples shown in this section in the program UsingEvents.cs
located in the ch11 directory. The listing for this program is omitted from this book for
brevity. If you compile and run that program, you'll see the order in which the events fire
when you add, modify, and remove a row from a DataTable that contains rows retrieved
from the Customers table.
The DataAdapter Events
The events exposed by a SqlDataAdapter object are shown in Table 11.11
.
Table 11.11: SqlDataAdapter EVENTS
EVENT EVENT HANDLER DESCRIPTION
FillError FillErrorEventHandler Fires when an error occurs during a call to the
Fill() method.
RowUpdating RowUpdatingEventHandler Fires before a row is added, modified, or
deleted in the database as a result of calling
the Update() method.
RowUpdated RowUpdatedEventHandler Fires after a row is added, modified, or
deleted in the database as a result of calling
the Update() method.
The FillError Event
The FillError event fires when you call the Fill() method and an error occurred. The
following are a couple of scenarios that would cause an error:
•
An attempt is made to add a number from the database to a DataColumn that
couldn't be converted to the .NET type of that DataColumn without losing
precision.
•
An attempt is made to add a row from the database to a DataTable that violates a
constraint in that DataTable.
The following example event handler, named FillErrorEventHandler(), checks for the
new FillErrorEventHandler(FillErrorEventHandler);
The RowUpdating Event
The RowUpdating event fires before a row is updated in the database as a result of you
calling the Update() method of your DataAdapter. This event fires once for each
DataRow you've added, modified, or deleted in a DataTable.
The following actions are performed behind the scenes for each DataRow when you call
the Update() method of your DataAdapter:
1. The values in your DataRow are copied to the parameter values of the appropriate
Command in the InsertCommand, UpdateCommand, or DeleteCommand property
of your DataAdapter.
2. The RowUpdating event of your DataAdapter fires.
3. The Command is run to push the change to the database.
4. Any output parameters from the Command are returned.
5. The RowUpdated event of your DataAdapter fires.
6. The AcceptChanges() method of your DataRow is called.
The second parameter to any event handler method you write to handle the RowUpdating
event of a SqlDataAdapter object is of the SqlRowUpdatingEventArgs class, and Table
11.13 shows the properties of this class.
Table 11.13: SqlRowUpdatingEventArgs PROPERTIES
PROPERTY TYPE DESCRIPTION
Command SqlCommand Gets or sets the SqlCommand that is run when the
Update() method is called.
Errors Exception Gets the Exception for any error that occurred.
Row DataRow Gets the DataRow to send to the database through
the Update() method.
StatementType StatementType Gets the type of the SQL statement that is to be run.
StatementType is an enumeration in the System.Data
namespace that contains the following members:
•
Delete
10, "Using DataSet Objects to Store Data").
The following example event handler, named RowUpdatingEventHandler(), prevents any
new rows from being added to the database with a CustomerID of J5COM:
public static void RowUpdatingEventHandler(
object sender, SqlRowUpdatingEventArgs mySRUEA
)
{
Console.WriteLine("\nIn RowUpdatingEventHandler()");
if ((mySRUEA.StatementType == StatementType.Insert) &&
(mySRUEA.Row["CustomerID"] == "J5COM"))
{
Console.WriteLine("Skipping current row");
mySRUEA.Status = UpdateStatus.SkipCurrentRow;
}
}
You indicate that mySqlDataAdapter is to call the RowUpdatingEventHandler() method
when the RowUpdating event fires using the following code:
mySqlDataAdapter.RowUpdating +=
new SqlRowUpdatingEventHandler(RowUpdatingEventHandler);
If you then call the AddDataRow() method shown earlier to attempt to add a row to the
Customers table, the RowUpdating event will fire and call the
RowUpdatingEventHandler() method. This method causes the new row to be skipped and
prevents it from being added to the Customers database table.
The RowUpdated Event
The RowUpdated event fires after a row is updated in the database as a result of you
calling the Update() method of your DataAdapter. This event fires once for each
DataRow you've added, modified, or deleted in a DataTable.
The second parameter to any method you write to handle the RowUpdated event of a
SqlDataAdapter object is of the SqlRowUpdatedEventArgs class. The properties of this
class are the same as those shown earlier in Table 11.13