ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

adplus-dvertising
Handling DataAdapter Events
Previous Home Next

DataAdapter supports only two events:

OnRowUpdating and OnRowUpdated.

These two events occur on either side of the actual dataset update, providing fine control of the process.

  1. OnRowUpdating Event
  2. The OnRowUpdating event is raised after the Update method has set the parameter values of the command to be executed but before the command is executed.The event handler for this event receives an argument whose properties provide essential information about the command that is about to be executed.

    The class of the event arguments is defined by the Data Provider, so it will be either OleDbRowUpdatingEventArgs or SqlRowUpdatingEventArgs if one of the .NET Framework Data Providers is used.

    RowUpdatingEventArgs Properties:

      Command: The Data Command to be executed.

      Errors: The errors generated by the .NET Data Provider

      Row: The DataReader to be updated

      StatementType: The type of Command tobe executed.The possiblevalues are Select, Insert Delete, and Update

      Status: The UpdateStatus of the Command

      TableMapping: The DataTableMap ping used by the update

  3. OnRowUpdated Event

The OnRowUpdated event is raised after the Update method executes the appropriate command against the data source. The event handler for this event is either passed an SqlRowUpdatedEventArgs or an OleDbRowUpdatedEventArgs argument, depending on the Data Provider.

Example:
// Assumes that connection is a valid SqlConnection object.
SqlDataAdapter sda = new SqlDataAdapter(
"SELECT EmpID, EmpName FROM Emp", connection);
// Add handlers.
sda.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
sda.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
// Set DataAdapter command properties, fill DataSet, modify DataSet.
sda.Update(ds, "Emp");
// Remove handlers.
sda.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating);
sda.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);
protected static void OnRowUpdating(
object sender, SqlRowUpdatingEventArgs args)
{
if (args.StatementType == StatementType.Delete)
{
System.IO.TextWriter tw = System.IO.File.AppendText("Deletes.log");
tw.WriteLine(
"{0}: Customer {1} Deleted.", DateTime.Now, 
 args.Row["EmpID", DataRowVersion.Original]);
tw.Close();
}
}
protected static void OnRowUpdated(
object sender, SqlRowUpdatedEventArgs args)
{
if (args.Status == UpdateStatus.ErrorsOccurred)
{
args.Row.RowError = args.Errors.Message;
args.Status = UpdateStatus.SkipCurrentRow;
}
}
Previous Home Next
>