OleDbDataAdapter.RowUpdating Event
Occurs during Update before a command is executed against the data source. The attempt to update is made, so the event fires.
[Visual Basic] Public Event RowUpdating As OleDbRowUpdatingEventHandler [C#] public event OleDbRowUpdatingEventHandler RowUpdating; [C++] public: __event OleDbRowUpdatingEventHandler* RowUpdating;
[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 OleDbRowUpdatingEventArgs containing data related to this event. The following OleDbRowUpdatingEventArgs properties provide information specific to this event.
| Property | Description |
|---|---|
| Command | Gets or sets the OleDbCommand to execute when performing the Update. |
| Errors (inherited from RowUpdatingEventArgs) | Gets any errors generated by the .NET Framework data provider when the Command executes. |
| Row (inherited from RowUpdatingEventArgs) | Gets the DataRow to send through an Update. |
| StatementType (inherited from RowUpdatingEventArgs) | Gets the type of SQL statement to execute. |
| Status (inherited from RowUpdatingEventArgs) | Gets the UpdateStatus of the Command property. |
| TableMapping (inherited from RowUpdatingEventArgs) | Gets the DataTableMapping to send through the Update. |
Remarks
When using Update, there are two events that occur per data row updated. The order of execution is as follows:
- The values in the DataRow are moved to the parameter values.
- The OnRowUpdating event is raised.
- The command executes.
- If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
- If there are output parameters, they are placed in the DataRow.
- The OnRowUpdated event is raised.
- 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
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