Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

OleDbDataAdapter.RowUpdated Event

Occurs during Update after a command is executed against the data source. The attempt to update is made, so the event fires.

[Visual Basic]
Public Event RowUpdated As OleDbRowUpdatedEventHandler
[C#]
public event OleDbRowUpdatedEventHandler RowUpdated;
[C++]
public: __event OleDbRowUpdatedEventHandler* RowUpdated;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type OleDbRowUpdatedEventArgs containing data related to this event. The following OleDbRowUpdatedEventArgs properties provide information specific to this event.

Property Description
Command Gets the OleDbCommand executed when Update is called.
Errors (inherited from RowUpdatedEventArgs) Gets any errors generated by the .NET Framework data provider when the Command was executed.
RecordsAffected (inherited from RowUpdatedEventArgs) Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.
Row (inherited from RowUpdatedEventArgs) Gets the DataRow sent through an Update.
StatementType (inherited from RowUpdatedEventArgs) Gets the type of SQL statement executed.
Status (inherited from RowUpdatedEventArgs) Gets the UpdateStatus of the Command property.
TableMapping (inherited from RowUpdatedEventArgs) Gets the DataTableMapping sent through an Update.

Remarks

When using Update, there are two events that occur per data row updated. The order of execution is as follows:

  1. The values in the DataRow are moved to the parameter values.
  2. The OnRowUpdating event is raised.
  3. The command executes.
  4. If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
  5. If there are output parameters, they are placed in the DataRow.
  6. The OnRowUpdated event is raised.
  7. AcceptChanges is called.

Example

[Visual Basic, C#, C++] The following example shows the RowUpdating and RowUpdated events in use.

[Visual Basic] 
Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class Sample

  public Shared Sub Main()
    Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

    Dim custDA As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Customers WHERE CustomerID = 'ALFKI'", nwindConn)
        
    custDA.InsertCommand = New OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)", nwindConn)

    custDA.InsertCommand.Parameters.Add("@CustomerID", OleDbType.VarChar, 5, "CustomerID")
    custDA.InsertCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 30, "CompanyName")

    nwindConn.Open()

    Dim custDS As DataSet = New DataSet()
    custDA.Fill(custDS, "Customers")

    Dim custRow As DataRow = custDS.Tables("Customers").NewRow()
    custRow("CustomerID") = "NEWCO"
    custRow("CompanyName") = "New Company"
    custDS.Tables("Customers").Rows.Add(custRow)

    ' add handlers
    AddHandler custDA.RowUpdating, New OleDbRowUpdatingEventHandler( AddressOf OnRowUpdating )
    AddHandler custDA.RowUpdated, New OleDbRowUpdatedEventHandler( AddressOf OnRowUpdated )

    custDA.Update(custDS, "Customers")

    ' remove handlers
    RemoveHandler custDA.RowUpdating, New OleDbRowUpdatingEventHandler( AddressOf OnRowUpdating )
    RemoveHandler custDA.RowUpdated, New OleDbRowUpdatedEventHandler( AddressOf OnRowUpdated )

    Dim myRow As DataRow
    For Each myRow In custDS.Tables("Customers").Rows
      If myRow.HasErrors Then Console.WriteLine(myRow.RowError)
    Next

    nwindConn.Close()
  End Sub

  protected Shared Sub OnRowUpdating(sender As Object, args As OleDbRowUpdatingEventArgs)

    If args.StatementType = StatementType.Insert Then
      Dim tw As System.IO.TextWriter = System.IO.File.AppendText("Inserts.log")
      tw.WriteLine("{0}: Customer {1} Inserted.", DateTime.Now, args.Row("CustomerID"))
      tw.Close()
    End If
  End Sub

  protected Shared Sub OnRowUpdated(sender As Object, args As OleDbRowUpdatedEventArgs)

    If args.Status = UpdateStatus.ErrorsOccurred Then
      args.Row.RowError = args.Errors.Message
      args.Status = UpdateStatus.SkipCurrentRow
    End If
  End Sub
End Class

[C#] 
using System;
using System.Data;
using System.Data.OleDb;

public class Sample
{

  public static void Main()
  {
    OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

    OleDbDataAdapter custDA = new OleDbDataAdapter("SELECT * FROM Customers WHERE CustomerID = 'ALFKI'", nwindConn);
        
    custDA.InsertCommand = new OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)", nwindConn);

    custDA.InsertCommand.Parameters.Add("@CustomerID", OleDbType.VarChar, 5, "CustomerID");
    custDA.InsertCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 30, "CompanyName");

    nwindConn.Open();

    DataSet custDS = new DataSet();
    custDA.Fill(custDS, "Customers");

    DataRow custRow = custDS.Tables["Customers"].NewRow();
    custRow["CustomerID"] = "NEWCO";
    custRow["CompanyName"] = "New Company";
    custDS.Tables["Customers"].Rows.Add(custRow);

    // add handlers
    custDA.RowUpdating += new OleDbRowUpdatingEventHandler( OnRowUpdating );
    custDA.RowUpdated += new OleDbRowUpdatedEventHandler( OnRowUpdated );

    custDA.Update(custDS, "Customers");

    // remove handlers
    custDA.RowUpdating -= new OleDbRowUpdatingEventHandler( OnRowUpdating );
    custDA.RowUpdated -= new OleDbRowUpdatedEventHandler( OnRowUpdated );

    foreach (DataRow myRow in custDS.Tables["Customers"].Rows)
    {
      if (myRow.HasErrors)
        Console.WriteLine(myRow.RowError);
    }

    nwindConn.Close();
  }

  protected static void OnRowUpdating(object sender, OleDbRowUpdatingEventArgs args)
  {
    if (args.StatementType == StatementType.Insert)
    {
      System.IO.TextWriter tw = System.IO.File.AppendText("Inserts.log");
      tw.WriteLine("{0}: Customer {1} Inserted.", DateTime.Now, args.Row["CustomerID"]);
      tw.Close();
    }
  }

  protected static void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
  {
    if (args.Status == UpdateStatus.ErrorsOccurred)
    {
      args.Row.RowError = args.Errors.Message;
      args.Status = UpdateStatus.SkipCurrentRow;
    }
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;

public __gc class Sample
{

public:
  static void OnRowUpdating(Object* /*sender*/, OleDbRowUpdatingEventArgs* args)
  {
    if (args->StatementType == StatementType::Insert)
    {
      System::IO::TextWriter* tw = System::IO::File::AppendText(S"Inserts.log");
      tw->WriteLine(S" {0}: Customer {1} Inserted.", __box(DateTime::Now), args->Row->Item[S"CustomerID"]);
      tw->Close();
    }
  }

  static void OnRowUpdated(Object* /*sender*/, OleDbRowUpdatedEventArgs* args)
  {
    if (args->Status == UpdateStatus::ErrorsOccurred)
    {
      args->Row->RowError = args->Errors->Message;
      args->Status = UpdateStatus::SkipCurrentRow;
    }
  }
};

int main()
{
  OleDbConnection* nwindConn = new OleDbConnection(S"Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

  OleDbDataAdapter* custDA = new OleDbDataAdapter(S"SELECT * FROM Customers WHERE CustomerID = 'ALFKI'", nwindConn);
  custDA->InsertCommand = new OleDbCommand(S"INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)", nwindConn);

  custDA->InsertCommand->Parameters->Add(S"@CustomerID", OleDbType::VarChar, 5, S"CustomerID");
  custDA->InsertCommand->Parameters->Add(S"@CompanyName", OleDbType::VarChar, 30, S"CompanyName");

  nwindConn->Open();

  DataSet* custDS = new DataSet();
  custDA->Fill(custDS, S"Customers");

  DataRow* custRow = custDS->Tables->Item[S"Customers"]->NewRow();
  custRow->Item[S"CustomerID"] = S"NEWCO";
  custRow->Item[S"CompanyName"] = S"New Company";
  custDS->Tables->Item[S"Customers"]->Rows->Add(custRow);

  // add handlers
  custDA->add_RowUpdating( new OleDbRowUpdatingEventHandler(0, Sample::OnRowUpdating) );
  custDA->add_RowUpdated( new OleDbRowUpdatedEventHandler(0, Sample::OnRowUpdated) );

  custDA->Update(custDS, S"Customers");

  // remove handlers
  custDA->remove_RowUpdating( new OleDbRowUpdatingEventHandler(0, Sample::OnRowUpdating) );
  custDA->remove_RowUpdated( new OleDbRowUpdatedEventHandler(0, Sample::OnRowUpdated) );

  System::Collections::IEnumerator* myEnum = custDS->Tables->Item[S"Customers"]->Rows->GetEnumerator();
  while (myEnum->MoveNext())
  {
    DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);

    if (myRow->HasErrors)
      Console::WriteLine(myRow->RowError);
  }

  nwindConn->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OleDbDataAdapter Class | OleDbDataAdapter Members | System.Data.OleDb Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.