OdbcRowUpdatingEventArgs Class

System.Data.Odbc

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

public OdbcRowUpdatingEventArgs(System.Data.DataRow row, System.Data.Statement completedStatement, System.Data.Common.DataTableMapping tableMapping, System.Data.Statement.StatementType statementType, System.Data.IDbTransaction transaction)
Initializes a new instance of the OdbcRowUpdatingEventArgs class.
public OdbcRowUpdatingEventArgs(System.Data.DataRow row, int recordNumber, System.Data.Statement completedStatement, System.Data.Common.DataTableMapping tableMapping, System.Data.Statement.StatementType statementType, System.Data.IDbTransaction transaction)
Initializes a new instance of the OdbcRowUpdatingEventArgs class with a record number.

Properties

public OdbcCommand UpdateCommand { get; }
Gets or sets the 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:

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.");
        // }
    }
}