vb.net qr code sample s WORKING WITH ADO.NET EVENTS in Visual Basic .NET

Generating Data Matrix ECC200 in Visual Basic .NET s WORKING WITH ADO.NET EVENTS

CHAPTER 15 s WORKING WITH ADO.NET EVENTS
DataMatrix Generator In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Read ECC200 In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
This event-handling process applies to all events in .NET. Consequently, ADO.NET objects, such as connections, data adapters, and datasets, all raise events that can be handled using the same process. We ll discuss the events raised by ADO.NET objects in detail throughout this chapter, but for now, let s get a feel for how all the different pieces of this process work. Connections support two events, InfoMessage and StateChange. To handle these events, include the WithEvents keyword when you declare the connection, for example:
Generating ANSI/AIM Code 39 In VB.NET
Using Barcode generation for .NET Control to generate, create Code 39 image in .NET applications.
www.OnBarcode.com
Generate Code128 In VB.NET
Using Barcode maker for .NET Control to generate, create Code-128 image in .NET framework applications.
www.OnBarcode.com
Friend WithEvents conn As System.Data.SqlClient.SqlConnection
Encoding GS1 - 13 In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create EAN-13 Supplement 5 image in .NET framework applications.
www.OnBarcode.com
Barcode Generator In Visual Basic .NET
Using Barcode creation for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Adding and Removing Event Handlers
Generate UPC Symbol In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
USPS POSTal Numeric Encoding Technique Barcode Maker In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create Delivery Point Barcode (DPBC) image in Visual Studio .NET applications.
www.OnBarcode.com
You specify an event handler with the Handles keyword. For example, you can bind the StateChange event of the connection object conn to the ConnStateChange method (which you write yourself) with:
Data Matrix Reader In C#.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Data Matrix Generator In None
Using Barcode creation for Word Control to generate, create ECC200 image in Office Word applications.
www.OnBarcode.com
Private Sub ConnStateChange( _ ByVal sender As Object, _ ByVal e As StateChangeEventArgs _ ) _ Handles conn.StateChange
Paint UPC A In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create UPC A image in ASP.NET applications.
www.OnBarcode.com
Draw EAN13 In Objective-C
Using Barcode generation for iPhone Control to generate, create EAN13 image in iPhone applications.
www.OnBarcode.com
Multiple events can be handled by the same handler, by specifying a commaseparated list of events after Handles. In this chapter, we ll discuss events raised by ADO.NET objects and how to handle them. Although events can be raised by other objects, this chapter deals only with events raised by ADO.NET objects.
Paint GTIN - 13 In .NET
Using Barcode maker for Visual Studio .NET Control to generate, create EAN13 image in VS .NET applications.
www.OnBarcode.com
Painting Code 128 Code Set A In Objective-C
Using Barcode creation for iPhone Control to generate, create Code128 image in iPhone applications.
www.OnBarcode.com
Raising and Handling ADO.NET Events
UPC-A Supplement 5 Reader In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
QR Code JIS X 0510 Generator In None
Using Barcode creation for Software Control to generate, create QR Code image in Software applications.
www.OnBarcode.com
Events are raised when a specific property of an object changes. Although objects can raise several events, we ll discuss only a few, because all can be handled in the same fashion. You ll use the System.Data.SqlClient .NET data provider, but all data providers handle events in a similar way.
Scan Code 39 In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Barcode Generator In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Working with Connection Object Events
Generating Universal Product Code Version A In C#.NET
Using Barcode creation for VS .NET Control to generate, create UPC-A Supplement 2 image in .NET applications.
www.OnBarcode.com
Barcode Printer In Java
Using Barcode maker for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
The connection object has two events: StateChange and InfoMessage. Let s look at these in more detail.
CHAPTER 15 s WORKING WITH ADO.NET EVENTS
Using the StateChange Event
The connection object raises the StateChange event when the state of the connection changes. The event handler receives a StateChangeEventArgs object, which you can examine for detailed information about the event. For example, it has OriginalState and CurrentState properties that you can access to determine the state of a connection before and after it changes.
Try It Out: Writing the Connection.StateChange Event
You ll now see how this event is handled. You ll open a connection to the Northwind database, retrieve one row from the Customers table, and close the connection. You ll write an event handler to notify you when the connection state changes. 1. In VBE, create a new Windows Application project named 15. When Solution Explorer opens, save the solution. 2. Rename the 15 project AdoNetEvents. Change the Text property of Form1 to ADO.NET Events. 3. Add four buttons, a label, and a list box to the form, as in Figure 15-1.
Figure 15-1. ADO.NET events form
4. Add an Imports statement to Form1.vb for the System.Data.SqlClient namespace. 5. Add the following declaration to the Form1 class:
Friend WithEvents conn As System.Data.SqlClient.SqlConnection
CHAPTER 15 s WORKING WITH ADO.NET EVENTS
6. Insert the code in Listing 15-1 into the click event handler for the first button.
Listing 15-1. Button1_Click()
' Create connection conn = New SqlConnection( _ "server = .\sqlexpress;" _ & "integrated security = true;" _ & "database = northwind" _ ) ' Create command Dim cmd As SqlCommand = conn.CreateCommand() cmd.CommandText = _ "select top 1 " _ & " customerid, " _ & " companyname " _ & "from " _ & " customers " cmd.Connection = conn Try ' Clear list box ListBox1.Items.Clear() ' Open connection - ConnStateChange event will be fired conn.Open() ' Create data reader Dim rdr As SqlDataReader = cmd.ExecuteReader() ' Display rows in list box While rdr.Read() ListBox1.Items.Add(rdr.GetString(0) & "-" & rdr.GetString(1)) End While Catch e1 As SqlException MessageBox.Show(e1.Message) Finally ' Close connection - ConnStateChange event will be fired conn.Close() End Try
CHAPTER 15 s WORKING WITH ADO.NET EVENTS
7. Add the method in Listing 15-2 to class Form1.cs.
Listing 15-2. ConnStateChange()
' Event handler for the StateChange Event Private Sub ConnStateChange( _ ByVal sender As Object, _ ByVal e As StateChangeEventArgs _ ) _ Handles conn.StateChange ListBox1.Items.Add("------------------------------") ListBox1.Items.Add("Entering StateChange Event Handler") ListBox1.Items.Add("Sender = " + sender.ToString()) ListBox1.Items.Add("Original State = " + e.OriginalState.ToString()) ListBox1.Items.Add("Current State = " + e.CurrentState.ToString()) ListBox1.Items.Add("Exiting StateChange Event Handler") ListBox1.Items.Add("------------------------------") End Sub
8. Build and run the solution with Ctrl+F5. Click the Connection StateChange Event button. You see the results in Figure 15-2.
Copyright © OnBarcode.com . All rights reserved.