OdbcRowUpdatingEventArgs Class
Provides data for the RowUpdating
event.
Syntax
public class OdbcRowUpdatingEventArgs : System.Data.RowUpdatingEventArgs
Inheritance
System.Object
System.EventArgs
System.Data.RowUpdatingEventArgs
System.Data.Odbc.OdbcRowUpdatingEventArgs
Constructors
OdbcRowUpdatingEventArgs
class.OdbcRowUpdatingEventArgs
class with a record number.Properties
OdbcCommand
to use during the update.Remarks
Overview
The RowUpdating
event is raised for each row being updated in the dataset. It allows you to intercept the update process and provide custom logic.
The OdbcRowUpdatingEventArgs
class provides access to the current row being updated, the update command, and the transaction. You can modify the UpdateCommand
property to execute a different command or add parameters. You can also set the Status
property to control the flow of the update process, such as continuing with the default update or rolling back the transaction.
The RowUpdating
event is typically handled when you need to perform complex update logic, such as:
- Generating primary keys for newly inserted rows.
- Performing custom validation before an update.
- Executing stored procedures with specific parameters.
- Handling concurrency conflicts.
Example
Handling the RowUpdating Event
using System;
using System.Data;
using System.Data.Odbc;
public class Sample
{
public static void Main(string[] args)
{
string connectionString = "DSN=myDsn;Uid=user;Pwd=password;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
OdbcDataAdapter adapter = new OdbcDataAdapter();
DataTable table = new DataTable();
// Configure adapter and fill table (omitted for brevity)
// Hook up event handler
adapter.RowUpdating += new OdbcRowUpdatingEventHandler(OnRowUpdating);
// ... perform updates to the DataTable ...
// Update the data source
try
{
adapter.Update(table);
}
catch (Exception ex)
{
Console.WriteLine("Update failed: " + ex.Message);
}
}
}
private static void OnRowUpdating(object sender, OdbcRowUpdatingEventArgs e)
{
Console.WriteLine("RowUpdating event triggered for Row State: " + e.Row.RowState.ToString());
// Example: Modify the UpdateCommand for Insert operations
if (e.Status == UpdateStatus.Continue && e.Row.RowState == DataRowState.Added)
{
OdbcCommand insertCommand = (OdbcCommand)e.UpdateCommand;
// You could add logic here to retrieve a new ID or modify parameters
Console.WriteLine("Intercepting insert operation.");
}
// Example: Set e.Status to Skip to skip the row update
// if (e.Row["SomeColumn"].ToString() == "Skip")
// {
// e.Status = UpdateStatus.SkipCurrentRow;
// Console.WriteLine("Skipping current row.");
// }
}
}