create barcode image c# Figure 13-3. Handled exception message in VB.NET

Creator ECC200 in VB.NET Figure 13-3. Handled exception message

Figure 13-3. Handled exception message
ECC200 Generation In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Read ECC200 In VB.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
CHAPTER 13 s HANDLING EXCEPTIONS
Barcode Creation In VB.NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Encoding USS-128 In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create EAN 128 image in VS .NET applications.
www.OnBarcode.com
10. When the message box in Figure 13-4 appears, click OK, then close the window.
PDF 417 Maker In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create PDF 417 image in .NET applications.
www.OnBarcode.com
Linear Barcode Creator In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Linear Barcode image in .NET framework applications.
www.OnBarcode.com
Figure 13-4. Message from Finally block
Encode UPC - 13 In VB.NET
Using Barcode generation for VS .NET Control to generate, create EAN13 image in Visual Studio .NET applications.
www.OnBarcode.com
Painting EAN-8 Supplement 5 Add-On In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create EAN8 image in .NET framework applications.
www.OnBarcode.com
How It Works
Data Matrix 2d Barcode Scanner In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Data Matrix 2d Barcode Generation In Java
Using Barcode printer for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
It would be highly unusual to miss setting the CommandText property. However, this was an expedient way to cause an ADO.NET exception. You specified that the command was for a stored procedure call, but you didn t specify the stored procedure to call:
Recognize Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Data Matrix 2d Barcode Reader In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
' Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure ' Deliberately fail to specify the procedure ' cmd.CommandText = "sp_Select_All_Employees"
ECC200 Creator In Java
Using Barcode creation for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
Decoding Code-39 In .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
So, when you called the ExecuteReader method, you got an exception, as shown in Figure 13-2. Though it was an unhandled exception, it still gave you an accurate diagnostic:
Encode European Article Number 13 In None
Using Barcode creator for Microsoft Word Control to generate, create EAN-13 image in Word applications.
www.OnBarcode.com
Print UCC - 12 In Java
Using Barcode encoder for Java Control to generate, create UPC-A Supplement 5 image in Java applications.
www.OnBarcode.com
ExecuteReader: CommandText property has not been initialized.
PDF417 Maker In None
Using Barcode encoder for Online Control to generate, create PDF417 image in Online applications.
www.OnBarcode.com
Create QR Code JIS X 0510 In Java
Using Barcode generator for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
It even gave you the option to continue or quit, but leaving this decision to users isn t a very good idea. After seeing what happened without handling the exception, you placed the call in a Try block:
Generate EAN128 In Java
Using Barcode encoder for Android Control to generate, create EAN / UCC - 14 image in Android applications.
www.OnBarcode.com
Make ECC200 In Objective-C
Using Barcode creator for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
Try ' Open connection conn.Open() ' Create data reader Dim rdr As SqlDataReader = cmd.ExecuteReader() ' Close reader rdr.Close()
CHAPTER 13 s HANDLING EXCEPTIONS
To handle the exception yourself, you coded two Catch clauses:
Catch ex As System.Data.SqlClient.SqlException Dim str As String = "Source: " & ex.Source str &= ControlChars.NewLine str &= "Exception Message: " & ex.Message MessageBox.Show(str, "Database Exception") Catch ex As System.Exception Dim str As String = "Source: " & ex.Source str &= ControlChars.NewLine str &= "Exception Message: " & ex.Message MessageBox.Show(str, "Non-Database Exception")
In the first Catch clause, you specified a database exception type. The second Catch clause, which produced the message box in Figure 13-3, is a generic block that catches all types of exceptions. Note the caption of the message box in this Catch block. It says NonDatabase Exception. Although you might think that a failure to specify a command string is a database exception, it s actually an ADO.NET exception; in other words, this error is trapped before it gets to the database server. So, when the button was clicked, because the CommandText property wasn t specified, an exception was thrown and caught by the second Catch clause. Even though a Catch clause for SqlException was provided, the exception was a System.InvalidOperationException, a common exception thrown by the CLR, not a database exception. The exception message indicates where the problem occurred: in the ExecuteReader method. The Finally block checks if the connection is open, and if it is, closes it and gives a message to that effect. Note that in handling the exception you did not terminate the application.
Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block closing the connection", "Finally") conn.Close() End If End Try
Try It Out: Handling an ADO.NET Exception (Part 2)
Let s handle another ADO.NET exception. You ll execute a stored procedure and then reference a nonexistent column in the returned dataset. This will throw an ADO.NET exception. This time, you ll code a specific Catch clause to handle the exception.
CHAPTER 13 s HANDLING EXCEPTIONS
1. You ll use the sp_Select_All_Employees stored procedure you created in 12. If you haven t already created it, please go to 12 and follow the steps in Try It Out: Creating and Executing a Trivial Stored Procedure. 2. Insert the code in Listing 13-3 into the body of the Button2_Click method.
Listing 13-3. Button2_Click()
' Create connection Dim conn As SqlConnection = New SqlConnection( _ "server = .\sqlexpress;" _ & "integrated security = true;" _ & "database = northwind" _ ) ' Create command Dim cmd As SqlCommand = conn.CreateCommand() ' Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_Select_All_Employees" Try ' Open connection conn.Open() ' Create data reader Dim rdr As SqlDataReader = cmd.ExecuteReader() ' Access nonexistent column Dim str As String = rdr.GetValue(20).ToString() ' Close reader rdr.Close() Catch ex As System.InvalidOperationException Dim str As String = "Source: " & ex.Source str &= ControlChars.NewLine str &= "Message: " & ex.Message str &= ControlChars.NewLine & ControlChars.NewLine str &= "Stack Trace: " & ex.StackTrace MessageBox.Show(str, "Specific Exception") Catch ex As System.Data.SqlClient.SqlException
Copyright © OnBarcode.com . All rights reserved.