vb.net qr code sample s HANDLING EXCEPTIONS in Visual Basic .NET

Printer DataMatrix in Visual Basic .NET s HANDLING EXCEPTIONS

CHAPTER 13 s HANDLING EXCEPTIONS
Data Matrix 2d Barcode Encoder In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Reader In Visual Basic .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
str str str str
Barcode Creator In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Printing PDF-417 2d Barcode In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
&= &= &= &=
DataMatrix Creation In VB.NET
Using Barcode generator for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
1D Barcode Maker In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create Linear image in Visual Studio .NET applications.
www.OnBarcode.com
ControlChars.NewLine "Line Number: " & ex.LineNumber.ToString() ControlChars.NewLine "Server: " & ex.Server.ToString()
Encoding EAN-13 In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create GTIN - 13 image in .NET applications.
www.OnBarcode.com
Case Code Generation In VB.NET
Using Barcode generation for .NET Control to generate, create Case Code image in VS .NET applications.
www.OnBarcode.com
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, "General Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block closing the connection", "Finally") conn.Close() End If End Try
Data Matrix Encoder In Objective-C
Using Barcode creator for iPad Control to generate, create Data Matrix image in iPad applications.
www.OnBarcode.com
Make DataMatrix In Java
Using Barcode generator for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
6. Run the program with Ctrl+F5, then click the Database Exception-1 button. You see the message box in Figure 13-8. Click OK to close the message box, then OK to close the next one, then close the window.
Barcode Generator In Java
Using Barcode encoder for BIRT reports Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Creating EAN 13 In Java
Using Barcode maker for Android Control to generate, create European Article Number 13 image in Android applications.
www.OnBarcode.com
Figure 13-8. RAISERROR database exception message
Generate PDF 417 In None
Using Barcode encoder for Online Control to generate, create PDF-417 2d barcode image in Online applications.
www.OnBarcode.com
Painting GTIN - 13 In None
Using Barcode encoder for Office Excel Control to generate, create EAN-13 Supplement 5 image in Office Excel applications.
www.OnBarcode.com
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 obtained this detailed information about the exception from the SqlException object.
Making Code-39 In Objective-C
Using Barcode encoder for iPad Control to generate, create Code39 image in iPad applications.
www.OnBarcode.com
PDF 417 Creation In Visual Studio .NET
Using Barcode creation for .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
How It Works
Encode EAN / UCC - 13 In .NET Framework
Using Barcode encoder for Reporting Service Control to generate, create UCC-128 image in Reporting Service applications.
www.OnBarcode.com
ANSI/AIM Code 39 Printer In Java
Using Barcode printer for Android Control to generate, create Code39 image in Android applications.
www.OnBarcode.com
In the sp_DbException_1 stored procedure, you first found the number of orders in the Orders table and stored the number in a variable called @ordercount:
Generate Data Matrix 2d Barcode In Objective-C
Using Barcode generation for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
www.OnBarcode.com
Read Quick Response Code In .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 13 s HANDLING EXCEPTIONS
select @ordercount = count(*) from orders
Then, if @ordercount was greater than ten, you raised 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 executed the stored procedure using the ExecuteNonQuery method within a Try block:
Try ' Open connection conn.Open() ' Execute stored procedure cmd.ExecuteNonQuery()
When the stored procedure executed, the RAISERROR statement raised an error, which ADO.NET converted to an exception. The exception was handled by:
Catch ex As System.Data.SqlClient.SqlException Dim str As String = "Source: " & ex.Source str &= ControlChars.NewLine str str str str str str str str str str str &= &= &= &= &= &= &= &= &= &= &= "Number: " & ex.Number.ToString() ControlChars.NewLine "Message: " & ex.Message ControlChars.NewLine "Class: " & ex.Class.ToString() ControlChars.NewLine "Procedure: " & ex.Procedure.ToString() ControlChars.NewLine "Line Number: " & ex.LineNumber.ToString() ControlChars.NewLine "Server: " & ex.Server.ToString()
MessageBox.Show(str, "Database Exception")
CHAPTER 13 s HANDLING EXCEPTIONS
Try It Out: Handling a Database Exception (Part 2): Stored Procedure Error
Now let s 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 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 13-5 into the Button4_Click method.
Listing 13-5. Button4_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_DbException_2" Try ' Open connection conn.Open()
CHAPTER 13 s HANDLING EXCEPTIONS
' Execute stored procedure cmd.ExecuteNonQuery() Catch ex As System.Data.SqlClient.SqlException Dim str As String = "Source: " & ex.Source str &= ControlChars.NewLine str &= "Number: " & ex.Number.ToString() str &= ControlChars.NewLine str &= "Message: " & ex.Message str &= ControlChars.NewLine str &= "Class: " & ex.Class.ToString() str &= ControlChars.NewLine str str str str str &= &= &= &= &= "Procedure: " & ex.Procedure.ToString() ControlChars.NewLine "Line Number: " & ex.LineNumber.ToString() ControlChars.NewLine "Server: " & ex.Server.ToString()
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, "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 with Ctrl+F5, then click the Database Exception-2 button. You see the message box in Figure 13-9. Click OK to close the message box, then OK to close the next one, then close the window.
Copyright © OnBarcode.com . All rights reserved.