SqlDataAdapter.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 SqlRowUpdatedEventHandler [C#] public event SqlRowUpdatedEventHandler RowUpdated; [C++] public: __event SqlRowUpdatedEventHandler* 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 SqlRowUpdatedEventArgs containing data related to this event. The following SqlRowUpdatedEventArgs properties provide information specific to this event.
| Property | Description |
|---|---|
| Command | Gets or sets the SqlCommand 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:
- 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 how to use both the RowUpdating and RowUpdated events.
[Visual Basic, C#, C++] The RowUpdating event returns this output:
[Visual Basic, C#, C++] event args: (command=System.Data.SqlClient.SqlCommand commandType=2 status=0)
[Visual Basic, C#, C++] The RowUpdated event returns this output:
[Visual Basic, C#, C++] event args: (command=System.Data.SqlClient.SqlCommand commandType=2 recordsAffected=1 row=System.Data.DataRow[37] status=0)
[Visual Basic] ' handler for RowUpdating event Protected Shared Sub OnRowUpdating(sender As Object, e As SqlRowUpdatingEventArgs) PrintEventArgs(e) End Sub 'OnRowUpdating ' handler for RowUpdated event Protected Shared Sub OnRowUpdated(sender As Object, e As SqlRowUpdatedEventArgs) PrintEventArgs(e) End Sub 'OnRowUpdated 'Entry point which delegates to C-style main Private Function Public Overloads Shared Sub Main() System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs()) End Sub Overloads Public Shared Function Main(args() As String) As Integer Const CONNECTION_STRING As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer" Const SELECT_ALL As String = "select * from Products" ' create DataAdapter Dim rAdapter As New SqlDataAdapter(SELECT_ALL, CONNECTION_STRING) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(rAdapter) ' Create and fill dataset (select only first 5 rows) Dim rDataSet As New DataSet() rAdapter.Fill(rDataSet, 0, 5, "Table") ' Modify dataSet Dim rTable As DataTable = rDataSet.Tables("Table") rTable.Rows(0)(1) = "new product" ' add handlers AddHandler rAdapter.RowUpdating, AddressOf OnRowUpdating AddHandler rAdapter.RowUpdated, AddressOf OnRowUpdated ' update, this operation fires two events (RowUpdating/RowUpdated) per changed row rAdapter.Update(rDataSet, "Table") ' remove handlers RemoveHandler rAdapter.RowUpdating, AddressOf OnRowUpdating RemoveHandler rAdapter.RowUpdated, AddressOf OnRowUpdated Return 0 End Function 'Main Overloads Protected Shared Sub PrintEventArgs(args As SqlRowUpdatingEventArgs) Console.WriteLine("OnRowUpdating") Console.WriteLine(" event args: (" & " command=" & args.Command.CommandText & _ " commandType=" & args.StatementType & " status=" & args.Status & ")") End Sub 'PrintEventArgs Overloads Protected Shared Sub PrintEventArgs(args As SqlRowUpdatedEventArgs) Console.WriteLine("OnRowUpdated") Console.WriteLine(" event args: (" & " command=" & args.Command.CommandText & _ " commandType=" & args.StatementType & " recordsAffected=" & _ args.RecordsAffected & " status=" & args.Status & ")") End Sub 'PrintEventArgs End Class 'Form1 [C#] // handler for RowUpdating event protected static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e) { PrintEventArgs(e); } // handler for RowUpdated event protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e) { PrintEventArgs(e); } public static int Main(String[] args) { const string CONNECTION_STRING = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; const string SELECT_ALL = "select * from Products"; // create DataAdapter SqlDataAdapter rAdapter = new SqlDataAdapter(SELECT_ALL, CONNECTION_STRING); SqlCommandBuilder cb = new SqlCommandBuilder(rAdapter); // Create and fill dataset (select only first 5 rows) DataSet rDataSet = new DataSet(); rAdapter.Fill(rDataSet, 0, 5, "Table"); // Modify dataSet DataTable rTable = rDataSet.Tables["Table"]; rTable.Rows[0][1] = "new product"; // add handlers rAdapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating ); rAdapter.RowUpdated += new SqlRowUpdatedEventHandler( OnRowUpdated ); // update, this operation fires two events (RowUpdating/RowUpdated) per changed row rAdapter.Update(rDataSet, "Table"); // remove handlers rAdapter.RowUpdating -= new SqlRowUpdatingEventHandler( OnRowUpdating ); rAdapter.RowUpdated -= new SqlRowUpdatedEventHandler( OnRowUpdated ); return 0; } protected static void PrintEventArgs(SqlRowUpdatingEventArgs args) { Console.WriteLine("OnRowUpdating"); Console.WriteLine(" event args: ("+ " command=" + args.Command + " commandType=" + args.StatementType + " status=" + args.Status + ")"); } protected static void PrintEventArgs(SqlRowUpdatedEventArgs args) { Console.WriteLine("OnRowUpdated"); Console.WriteLine( " event args: ("+ " command=" + args.Command + " commandType=" + args.StatementType + " recordsAffected=" + args.RecordsAffected + " status=" + args.Status + ")" ); } [C++] protected: // handler for RowUpdating event static void OnRowUpdating(Object* /*sender*/, SqlRowUpdatingEventArgs* e) { PrintEventArgs(e); } // handler for RowUpdated event static void OnRowUpdated(Object* /*sender*/, SqlRowUpdatedEventArgs* e) { PrintEventArgs(e); } public: static int Main() { String* CONNECTION_STRING = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; String* SELECT_ALL = S"select * from Products"; // create DataAdapter SqlDataAdapter* rAdapter = new SqlDataAdapter(SELECT_ALL, CONNECTION_STRING); SqlCommandBuilder* cb = new SqlCommandBuilder(rAdapter); // Create and fill dataset (select only first 5 rows) DataSet* rDataSet = new DataSet(); rAdapter->Fill(rDataSet, 0, 5, S"Table"); // Modify dataSet DataTable* rTable = rDataSet->Tables->Item[S"Table"]; rTable->Rows->Item[0]->Item[1] = S"new product"; // add handlers rAdapter->RowUpdating += new SqlRowUpdatingEventHandler(0, OnRowUpdating ); rAdapter->RowUpdated += new SqlRowUpdatedEventHandler(0, OnRowUpdated ); // update, this operation fires two events (RowUpdating/RowUpdated) per changed row rAdapter->Update(rDataSet, S"Table"); // remove handlers rAdapter->RowUpdating -= new SqlRowUpdatingEventHandler(0, OnRowUpdating ); rAdapter->RowUpdated -= new SqlRowUpdatedEventHandler(0, OnRowUpdated ); return 0; } protected: static void PrintEventArgs(SqlRowUpdatingEventArgs* args) { Console::WriteLine(S"OnRowUpdating"); Console::WriteLine(String::Format( S" event args: ( command={0} commandType={1} status={2})", args->Command, __box(args->StatementType), __box(args->Status))); } static void PrintEventArgs(SqlRowUpdatedEventArgs* args) { Console::WriteLine(S"OnRowUpdated"); Console::WriteLine(String::Concat( String::Format( S" event args: ( command={0} commandType={1} ", args->Command, __box(args->StatementType) ), String::Format( S"recordsAffected={0} status={1})", __box(args->RecordsAffected), __box(args->Status) ))); }
[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
SqlDataAdapter Class | SqlDataAdapter Members | System.Data.SqlClient Namespace