Accessing Databases with ADO.NET, Handling Exceptions, and Printing Chapter 10 Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Objectives Use the MonthCalendar control in an application Use the PrintDialog control in an application Use the PrintDocument control in an application Write code in a class Understand database connections Write code to connect to a database
Objectives Understand how the CLR handles exceptions during run time Code a Try…Catch…Finally statement to handle exceptions Read records from a database Understand the role of SQL in interacting with a database Use simple SQL SELECT statements to query a database Understand the functionality and major components of Crystal Reports
Creating the Compensation Review Report Application
Starting Visual Basic .NET and Adding a New Class to the Compensation Review Class Library Insert the Data Disk in drive A. Using Windows Explorer, copy the Compensation Review folder from the A:\Chapter9 folder to the A:\Chapter10 folder Start Visual Basic .NET. When the Start Page appears, click the Open Project button on the Start Page If necessary, click the Look in box arrow and then click 3½ Floppy (A:). Double-click the Chapter10 folder Double-click the Compensation Review folder. If necessary, click the Compensation Review solution file (Compensation Review.sln)
Starting Visual Basic .NET and Adding a New Class to the Compensation Review Class Library Click the Open button. When the Compensation Review project appears in the Solution Explorer window, right-click the Compensation Review project. When the shortcut menu appears, point to the Add command and then click Add Class on the Add menu Select the text in the Name box and type Employees.vb as then new name. Click the Open button
Declaring Private Variables and Creating a Database Connection String If necessary, double-click the Employees.vb class module in the Solution Explorer window. When the Employees.vb code window opens, type the code as shown below:
Declaring Private Variables and Creating a Database Connection String Type the code as shown below:
Try…Catch Exception Handling
Try…Catch Exception Handling
Preparing a Data Command and a Data Reader and Coding a Try…Catch Statement Enter lines 24 through 32, as shown below:
Connecting to a Database Type lines 12 through 24, as shown below:
Coding a SQL SELECT Statement to Read Hourly Employee Records Type lines 24 through 34, as shown below:
Completing Coding the SQL SELECT Statement Type lines 33 and 34, as shown below:
Using a Loop to Read Hourly Employee Records Type lines 35 through 48, as shown below:
Coding a SQL SELECT Statement and Looping to Read Salaried Employee Records Type lines 50 through 65, as shown below:
Building, Saving, and Documenting the Class Library Right-click the Compensation Review project in the Solution Explorer window. When the shortcut menu appears, click the Build command Use the Print command on the File menu to print a record of the code for the Employees class
Adding a New Project to a Solution Right-click the Compensation Review solution in the Solution Explorer. When the shortcut menu appears, point to the Add command Click the New Project command When the Add New Project dialog box appears, if necessary, click the Windows Application template in the Templates area. Select the text in the Name box, type Compensation Review Report as the new project name, and then click the OK button
Adding a New Project to a Solution Right-click the References folder for the Compensation Review Report and then click the Add Reference command on the References shortcut menu. When the Add Reference dialog box appears, click the Browse button and use the Select Component dialog box to add a reference to the Compensation Review.dll component located in the A:\Chapter10\Compensation Review\Compensation Review\bin folder
Setting Form Properties and Adding Controls With Form1 selected, set the properties of the Form1 form as specified in Table 10-4 on page VB 10.42. Add the controls to the form as shown below and set the property values of the controls as listed in Table 10-5 on pages VB 10.42 and VB 10.43. Add the MonthCalendar control to the GroupBox1 control. Add the two RadioButton controls to the GroupBox2 control
Changing the Properties of a MonthCalendar Control Click the MonthCalendar control. Change the Name property value for the MonthCalendar control to calReviewDate, as well as the remaining properties of the control as listed below:
Changing the Properties of PrintDialog and PrintDocument Controls Click the PrintDialog control in the Component Tray. Change the Name property value of the PrintDialog control as shown below Click the PrintDocument control in the Component Tray. Change the Name property value of the PrintDocument control as shown below
Using a PrintDialog Control and a PrintDocument Control in Code Double-click the btnPrint Button control on the Form1 form. When the code window appears, type the code as shown below:
Using a PrintDialog Control and a PrintDocument Control in Code Enter lines 135 through 139 as shown below. Visual Basic .NET automatically adds lines 134 and 140 to the code
Building a Report Header Type lines 142 through 170, as shown below:
Building the Report Body Enter lines 172 through 175 and 177 through 189, as shown below:
Finishing Building the Report Body Type lines 190 through 203, as shown below:
Sending Output to a Printer and Catching an Exception Click the Form1.vb[Design]* tab at the top of the code window. When the Form1 form appears, double-click the objPrintDocument control in the Component Tray Type lines 207 through 229, as shown on the following slide
Sending Output to a Printer and Catching an Exception
Setting the Startup Project, Saving the Project, and Testing the Project Click the Compensation Review solution in the Solution Explorer. Click the property value next to the Startup project property in the Properties window. Click the box arrow in the property value of the Startup project Click Compensation Review Report in the list box Click the Save All button on the Standard toolbar Click the Start button on the Visual Basic .NET Standard toolbar
Setting the Startup Project, Saving the Project, and Testing the Project When the Compensation Review Report application window appears, use the arrow buttons at the top of the MonthCalendar control to navigate the months and then select July 30, 2004 using the MonthCalendar control Click the Print Compensation Review Report button. When the Print dialog box appears, select a printer and then click the OK button Click the Close button on the Compensation Review Report application window title bar. If necessary, click the Visual Basic .NET taskbar button to display the Visual Basic .NET window. If necessary, close the Output window
Documenting the Application and Quitting Visual Basic .NET Click the Form1.vb[Design] tab and then use the PRINT SCREEN key to print a record of the user interface design of the Form1 form Click the Form1.vb tab. Click File on the menu bar and then click Page Setup When the Page Setup dialog box appears, if necessary, click Line numbers and then click the OK button Use the Print command on the File menu to print a record of the code for the Compensation Review Report application Click the Visual Basic .NET Close button
Adding a Crystal Report to a Project Add a Crystal Report module to a Windows application project by right-clicking the appropriate project in the Solution Explorer, pointing to Add on the shortcut menu, clicking Add New Item on the Add menu, and adding a report entitled CrystalReport1.rpt
Adding a Crystal Report to a Project Click the Open button on the Add New Item dialog box
Using a Report Expert to Create a Report Choose the Standard Report Expert and click OK
Using a Report Expert to Create a Report Select the OLE DB (ADO) data source, and choose the second item in the provider list
Using a Report Expert to Create a Report
Using a Report Expert to Create a Report On this screen, you are able to fill in any additional, necessary database parameters
Using a Report Expert to Create a Report Select the tables you wish to appear in the report, and click the Next button
Using a Report Expert to Create a Report If necessary, you can add additional links to the tables on the Links tab before clicking the Next button
Using a Report Expert to Create a Report The Fields tab allows you to specify which fields from the tables to print on the report
Using a Report Expert to Create a Report After clicking the Finish button on the Standard Report Expert dialog box, you are able to view and modify the report layout
Using the CrystalReportViewer Control
Running a Crystal Reports Report
Summary Use the MonthCalendar control in an application Use the PrintDialog control in an application Use the PrintDocument control in an application Write code in a class Understand database connections Write code to connect to a database
Summary Understand how the CLR handles exceptions during run time Code a Try…Catch…Finally statement to handle exceptions Read records from a database Understand the role of SQL in interacting with a database Use simple SQL SELECT statements to query a database Understand the functionality and major components of Crystal Reports
Chapter 10 Complete