Chapter 5: Handling ADO.NET Events


Overview

Events are useful for notifying you when some action happens to an object. An event can have a method, called an event handler, associated with it. When an event occurs, the associated event handler (method) is executed. An event can have multiple event handlers. In Visual Basic .NET (VB .NET), you use the AddHandler and RemoveHandler statements to add and remove event handlers.

AddHandler takes two arguments: the name of an event from an object and an expression that evaluates to a delegate. You use the AddressOf statement to pass a reference to the delegate. For example, the following code adds an event handler:

 AddHandler MyObject.Event1, AddressOf Me.MyEventHandler 

RemoveHandler disconnects an event from an event handler, and it uses the same syntax as AddHandler. For example, the following code removes the event handler:

 RemoveHandler MyObject.Event1, AddressOf Me.MyEventHandler 

Note

If you want to learn more about events and delegates in VB .NET, see the VB .NET language reference in the documentation.

In this chapter, you'll learn how you can handle events for ADO.NET objects. As discussed in the past two chapters, you can divide ADO.NET classes into two groups: disconnected classes and connected classes. The System.Data namespace defines common event handlers and event argument classes for all data providers. As you can see from Figure 5-1, event handler classes are derived from the Delegate class and event argument classes are derived from the EventArgs class.


Figure 5-1: Event handler and event argument classes defined in the System.Data namespace

Note

ADO.NET data providers are derived from a common hierarchy, so all data providers support the same set of events.

DataColumnChangeEventHandler is an event handler for DataColumn when a value is being changed for a column in a DataRow. DataRowChangeEventHandler is an event handler that handles the RowChanging, RowChanged, RowDeleting, and RowDeleted events of a DataTable. The FillErrorEventHandler is an event handler that handles a DataAdapter's Fill event. If an error occurred during the fill operation, the FillError fires, which executes FillErrorEventHandler. MergeFailedEventHandler handles the DataSet's MergeFailed event that occurs when a DataSet merge operation fails. Whenever a connection's state changes, the StateChange event occurs. StateChangeEventHandler handles this event.

Besides the event handler and event argument classes defined in the System.Data namespace, the data provider event handler and event argument classes are defined in the data provider-specific namespace. Figure 5-2 shows the OleDb data provider event handlers and event argument classes.

click to expand
Figure 5-2: OleDb data provider event handler and event arguments

OleDbInfoMessageEventHandler handles the InfoMessage event of the OleDbConnection. An InfoMessage event occurs when a provider sends a warning or an informational message to the application.

The RowUpdated event occurs when an Update method is called on a DataAdapter. OleDbRowUpdatedEventHandler handles the RowUpdated event of the OleDbDataAdapter. Apart from the RowUpdated event, there's one more update-related event: RowUpdating. As you can guess from its name, this event occurs when a row is being updated, which means before the command is executed against a data source. OleDbRowUpdatingEventHandler is responsible for handling this event.

In this chapter, you'll use the Sql data provider.




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net