vb.net qr code sample s HANDLING EXCEPTIONS in VB.NET

Generation DataMatrix in VB.NET s HANDLING EXCEPTIONS

CHAPTER 16 s HANDLING EXCEPTIONS
Drawing DataMatrix In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Decoding ECC200 In VB.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
6. Run the program by pressing Ctrl+F5, and then click the Database Exception-1 button. You ll see the message box in Figure 16-8. Click OK to close the message box, click OK to close the next one, and then close the window.
Generate Barcode In VB.NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
UCC - 12 Generation In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create UPC-A Supplement 5 image in Visual Studio .NET applications.
www.OnBarcode.com
Figure 16-8. RAISERROR database exception message Observe the caption and contents of the message box. The source, message, name of the stored procedure, exact line number where the error was found, and name of the server are all displayed. You obtain this detailed information about the exception from the SqlException object.
Encoding ECC200 In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
Encoding PDF 417 In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
How It Works
Creating USS-128 In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create GTIN - 128 image in VS .NET applications.
www.OnBarcode.com
Painting Leitcode In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Leitcode image in Visual Studio .NET applications.
www.OnBarcode.com
In the sp_DBException_1 stored procedure, you first find the number of orders in the Orders table and store the number in a variable called @ordercount: select @ordercount = count(*) from orders If @ordercount is greater than ten, you raise an error using the RAISERROR statement: if @ordercount > 10 raiserror ( 'Orders Count is greater than 10 - Notify the Business Manager', 16, 1 ) Then, in the button3_Click method, you execute the stored procedure using the ExecuteNonQuery method within a Try block: Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery()
Generating Data Matrix 2d Barcode In Java
Using Barcode creator for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
Creating Data Matrix 2d Barcode In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
CHAPTER 16 s HANDLING EXCEPTIONS
USS Code 39 Printer In Java
Using Barcode generation for Android Control to generate, create Code-39 image in Android applications.
www.OnBarcode.com
Create UPC A In .NET
Using Barcode drawer for Reporting Service Control to generate, create UPC-A image in Reporting Service applications.
www.OnBarcode.com
When the stored procedure executes, the RAISERROR statement raises an error, which is converted to an exception by ADO.NET. The exception is handled by Catch ex As System.Data.SqlClient.SqlException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Number: " + ex.Number.ToString str += ControlChars.NewLine + "Message: " + ex.Message str += ControlChars.NewLine + "Class: " + ex.Class.ToString str += ControlChars.NewLine + "Procedure: " + ex.Procedure str += ControlChars.NewLine + "Line Number: " + ex.LineNumber.ToString str += ControlChars.NewLine + "Server: " + ex.Server MessageBox.Show(str, "Database Exception")
Generating Data Matrix ECC200 In Java
Using Barcode encoder for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
USS Code 39 Creation In Objective-C
Using Barcode drawer for iPhone Control to generate, create Code39 image in iPhone applications.
www.OnBarcode.com
Try It Out: Handling a Database Exception (Part 2): Stored Procedure Error
Drawing ANSI/AIM Code 128 In .NET
Using Barcode maker for .NET Control to generate, create Code 128 Code Set B image in VS .NET applications.
www.OnBarcode.com
Barcode Encoder In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Now you ll see what happens when a statement in a stored procedure encounters an error. You ll create a stored procedure that attempts an illegal INSERT, and then you ll extract information from the SqlException object. 1. Using SSMSE, create a stored procedure in Northwind named sp_DbException_2, as follows: create procedure sp_DBException_2 as set nocount on insert into employees ( employeeid, Firstname ) values (50, 'Cinderella') 2. Insert the code in Listing 16-5 into the button4_Click method. Listing 16-5. button4_Click() Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\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_DbException_2"
Painting Linear In Java
Using Barcode generation for Java Control to generate, create Linear 1D Barcode image in Java applications.
www.OnBarcode.com
Paint Barcode In Java
Using Barcode creation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
CHAPTER 16 s HANDLING EXCEPTIONS
Scan Code39 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
EAN / UCC - 13 Recognizer In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery() Catch ex As System.Data.SqlClient.SqlException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Number: " + ex.Number.ToString str += ControlChars.NewLine + "Message: " + ex.Message str += ControlChars.NewLine + "Class: " + ex.Class.ToString str += ControlChars.NewLine + "Procedure: " + ex.Procedure str += ControlChars.NewLine + "Line Number: " + ex.LineNumber.ToString str += ControlChars.NewLine + "Server: " + ex.Server MessageBox.Show(str, "Database Exception") Catch ex As System.Exception Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "General Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block Closing the connection", "Finally") conn.Close() End If End Try 3. Run the program by pressing Ctrl+F5, and then click the Database Exception-2 button. You ll see the message box in Figure 16-9. Click OK to close the message box, click OK to close the next one, and then close the window.
Figure 16-9. Stored procedure database exception message
CHAPTER 16 s HANDLING EXCEPTIONS
How It Works
The stored procedure tries to insert a new employee into the Employees table: insert into employees ( employeeid, firstname ) values (50, 'Cinderella') However, since the EmployeeID column in the Employees table is an IDENTITY column, you can t explicitly assign a value to it.
s Actually, you can as the message indicates if you use SET Tip
IDENTITY_INSERT employees OFF
in the stored procedure before you attempt the INSERT. This would allow you to insert explicit EmployeeID values, but this seldom is, or should be, done.
When this SQL error occurs, the specific SqlException Catch clause traps it and displays the information. The Finally block then closes the connection. It s possible for stored procedures to encounter several errors. You can trap and debug these using the SqlException object, as you ll see next.
Copyright © OnBarcode.com . All rights reserved.