Connection Class Events

Team-Fly team-fly    

ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 5.   Connecting to Databases

The ADO .NET approach to events is a complete reversal of the ADO approach. ADO had events for each step of the process: WillConnect, Connected, AlmostConnected. (Just kidding about that last one; but it did have too many events.) The ADO .NET people went in the opposite direction. The ADO .NET Connection classes have only two events. Actually, all of the .Net framework is light on events. Instead of different events, the ADO .NET designers use different EventArgs object types to pass more specific information about why the event was fired . The Connection classes are no different.

The StateChanged Event

The StateChanged event fires whenever the Connection object's State property changes. If you need to know the exact moment a connection is established, you can use this event. It passes an EventArgs object of type StateChangeEventArgs. This object has two properties: CurrentState and OriginalState. These each have a value of type System.Data.ConnectionState. You can test these properties and take action based on their values.

We can create a simple application to demonstrate this. Let's use our ADOBook05-01 project. Add two command buttons to the form and one list box. Your form should look like Figure 5.7.

Figure 5.7. Form for demonstrating the StateChanged event.


You should already have an OleDbConnection object on the form. If not, drop one on now. Let's set it up to connect to the Northwind database. Drop down the list for the ConnectionString property on the properties window. Either select an existing connection or create one. If you create one, fill in the Properties page as illustrated in Figure 5.8.

Figure 5.8. Data Link Properties with Northwind selected.


Of course you would substitute the name of your server. Next, double-click the first button (Button1). The code window opens and a click event procedure has been created for you. Add the following code to the procedure.

 Private Sub Button1_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles Button1.Click      OleDbConnection1.Open()  End Sub 

In the Button2 click event, add the following code:

 Private Sub Button2_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles Button2.Click      OleDbConnection1.Close()  End Sub 

Next create the StateChanged event for the Connection object. To do this, drop down the Class Name list (on the left) and select the OleDbConnection1 object. Then drop down the Method list on the right and select the StateChanged event. The IDE will create an event procedure for you. Place the following code in the event procedure:

 Private Sub OleDbConnection1_StateChange(ByVal sender As Object, ByVal  e As System.Data.StateChangeEventArgs) _  Handles OleDbConnection1.StateChange            ListBox1.Items.Add("Original State = " &  e.OriginalState.ToString & ", Current State = " _   & e.CurrentState.ToString)       End Sub 

When we run the program, this should display the state of the connection in the list box. Run the program. Click Button1. This opens the connection. Then click Button2. This closes the connection. After you have clicked both buttons, the screen should look like Figure 5.9.

Figure 5.9. StateChanged event demonstration.


Save this project as ADOBook05-01 if you have not already done so. This is its index on the CD.

The InfoMessage Event

The InfoMessage event fires when a message with low severity is returned from the data source. Low severity messages are those that do not result in an exception. For Microsoft SQL Server, this includes error messages with a severity of 10 or less. For other providers this is system-dependent.

The InfoMessage event passes an EventArgs object of type OleDbInfoMessageEventArgs. It has the following properties:

  • ErrorCode The ANSI SQL compliant error code of the message.

  • Errors A collection of OleDbError objects sent from the source.

  • Message The full text of the message sent from the server. In case of multiple messages, the first one in the collection is sent in this property. You should iterate through the Errors collection for other messages.

  • Source The name of the object that caused the message.

Team-Fly team-fly    

ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: