- •About the Authors
- •Contents at a Glance
- •Contents
- •Introduction
- •Goal of the Book
- •How to Use this Book
- •Introduction to the .NET Framework
- •Common Language Runtime (CLR)
- •Class Library
- •Assembly
- •Versioning
- •Exceptions
- •Threads
- •Delegates
- •Summary
- •Introduction to C#
- •Variables
- •Initializing Variables
- •Variable Modifiers
- •Variable Data Types
- •Types of Variables
- •Variable Scope
- •Types of Data Type Casting
- •Arrays
- •Strings
- •Initializing Strings
- •Working with Strings
- •Statements and Expressions
- •Types of Statements
- •Expressions
- •Summary
- •Classes
- •Declaring Classes
- •Inheritance
- •Constructors
- •Destructors
- •Methods
- •Declaring a Method
- •Calling a Method
- •Passing Parameters to Methods
- •Method Modifiers
- •Overloading a Method
- •Namespaces
- •Declaring Namespaces
- •Aliases
- •Structs
- •Enumerations
- •Interfaces
- •Writing, Compiling, and Executing
- •Writing a C# Program
- •Compiling a C# Program
- •Executing a C# Program
- •Summary
- •Arrays
- •Single-Dimensional Arrays
- •Multidimensional Arrays
- •Methods in Arrays
- •Collections
- •Creating Collections
- •Working with Collections
- •Indexers
- •Boxing and Unboxing
- •Preprocessor Directives
- •Summary
- •Attributes
- •Declaring Attributes
- •Attribute Class
- •Attribute Parameters
- •Default Attributes
- •Properties
- •Declaring Properties
- •Accessors
- •Types of Properties
- •Summary
- •Introduction to Threads
- •Creating Threads
- •Aborting Threads
- •Joining Threads
- •Suspending Threads
- •Making Threads Sleep
- •Thread States
- •Thread Priorities
- •Synchronization
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Primary and Foreign Keys
- •Referential Integrity
- •Normalization
- •Designing a Database
- •Low-Level Design
- •Construction
- •Integration and Testing
- •User Acceptance Testing
- •Implementation
- •Operations and Maintenance
- •Summary
- •Creating a New Project
- •Console Application
- •Windows Applications
- •Creating a Windows Application for the Customer Maintenance Project
- •Creating an Interface for Form1
- •Creating an Interface for WorkerForm
- •Creating an Interface for CustomerForm
- •Creating an Interface for ReportsForm
- •Creating an Interface for JobDetailsForm
- •Summary
- •Performing Validations
- •Identifying the Validation Mechanism
- •Using the ErrorProvider Control
- •Handling Exceptions
- •Using the try and catch Statements
- •Using the Debug and Trace Classes
- •Using the Debugging Features of Visual Studio .NET
- •Using the Task List
- •Summary
- •Creating Form1
- •Connecting WorkerForm to the Workers Table
- •Connecting CustomerForm to the tblCustomer Table
- •Connecting the JobDetails Form
- •to the tblJobDetails Table
- •Summary
- •Introduction to the Crystal Reports Designer Tool
- •Creating the Reports Form
- •Creating Crystal Reports
- •Creating the Windows Forms Viewer Control
- •Creating the Monthly Worker Report
- •Summary
- •Introduction to Deploying a Windows Application
- •Deployment Projects Available in Visual Studio .NET
- •Deployment Project Editors
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Populating the TreeView Control
- •Displaying Employee Codes in the TreeView Control
- •Event Handling
- •Displaying Employee Details in the ListView Control
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Adding the Programming Logic to the Application
- •Adding Code to the Form Load() Method
- •Adding Code to the OK Button
- •Adding Code to the Exit Button
- •Summary
- •The Created Event
- •Adding Code to the Created Event
- •Overview of XML
- •The XmlReader Class
- •The XmlWriter Class
- •Displaying Data in an XML Document
- •Displaying an Error Message in the Event Log
- •Displaying Event Entries from Event Viewer
- •Displaying Data from the Summary.xml Document in a Message Box
- •Summary
- •Airline Profile
- •Role of a Business Manager
- •Role of a Network Administrator
- •Role of a Line-of-Business Executive
- •Project Requirements
- •Creation and Deletion of User Accounts
- •Addition of Flight Details
- •Reservations
- •Cancellations
- •Query of Status
- •Confirmation of Tickets
- •Creation of Reports
- •Launch of Frequent Flier Programs
- •Summarizing the Tasks
- •Project Design
- •Database Design
- •Web Forms Design
- •Enabling Security with the Directory Structure
- •Summary
- •Getting Started with ASP.NET
- •Prerequisites for ASP.NET Applications
- •New Features in ASP.NET
- •Types of ASP.NET Applications
- •Exploring ASP.NET Web Applications
- •Introducing Web Forms
- •Web Form Server Controls
- •Configuring ASP.NET Applications
- •Configuring Security for ASP.NET Applications
- •Deploying ASP.NET Applications
- •Creating a Sample ASP.NET Application
- •Creating a New Project
- •Adding Controls to the Project
- •Coding the Application
- •Summary
- •Creating the Database Schema
- •Creating Database Tables
- •Managing Primary Keys and Relationships
- •Viewing the Database Schema
- •Designing Application Forms
- •Standardizing the Interface of the Application
- •Common Forms in the Application
- •Forms for Network Administrators
- •Forms for Business Managers
- •Forms for Line-of-Business Executives
- •Summary
- •The Default.aspx Form
- •The Logoff.aspx Form
- •The ManageUsers.aspx Form
- •The ManageDatabases.aspx Form
- •The ChangePassword.aspx Form
- •Restricting Access to Web Forms
- •The AddFl.aspx Form
- •The RequestID.aspx Form
- •The Reports.aspx Form
- •The FreqFl.aspx Form
- •Coding the Forms for LOB Executives
- •The CreateRes.aspx Form
- •The CancelRes.aspx Form
- •The QueryStat.aspx Form
- •The ConfirmRes.aspx Form
- •Summary
- •Designing the Form
- •The View New Flights Option
- •The View Ticket Status Option
- •The View Flight Status Option
- •The Confirm Reservation Option
- •Testing the Application
- •Summary
- •Locating Errors in Programs
- •Watch Window
- •Locals Window
- •Call Stack Window
- •Autos Window
- •Command Window
- •Testing the Application
- •Summary
- •Managing the Databases
- •Backing Up the SkyShark Airlines Databases
- •Exporting Data from Databases
- •Examining Database Logs
- •Scheduling Database Maintenance Tasks
- •Managing Internet Information Server
- •Configuring IIS Error Pages
- •Managing Web Server Log Files
- •Summary
- •Authentication Mechanisms
- •Securing a Web Site with IIS and ASP.NET
- •Configuring IIS Authentication
- •Configuring Authentication in ASP.NET
- •Securing SQL Server
- •Summary
- •Deployment Scenarios
- •Deployment Editors
- •Creating a Deployment Project
- •Adding the Output of SkySharkDeploy to the Deployment Project
- •Deploying the Project to a Web Server on Another Computer
- •Summary
- •Organization Profile
- •Project Requirements
- •Querying for Information about All Books
- •Querying for Information about Books Based on Criteria
- •Ordering a Book on the Web Site
- •Project Design
- •Database Design
- •Database Schema
- •Web Forms Design
- •Flowcharts for the Web Forms Modules
- •Summary
- •Introduction to ASP.NET Web Services
- •Web Service Architecture
- •Working of a Web Service
- •Technologies Used in Web Services
- •XML in a Web Service
- •WSDL in a Web Service
- •SOAP in a Web Service
- •UDDI in a Web Service
- •Web Services in the .NET Framework
- •The Default Code Generated for a Web Service
- •Testing the SampleWebService Web Service
- •Summary
- •Creating the SearchAll() Web Method
- •Creating the SrchISBN() Web Method
- •Creating the AcceptDetails() Web Method
- •Creating the GenerateOrder() Web Method
- •Testing the Web Service
- •Securing a Web Service
- •Summary
- •Creating the Web Forms for the Bookers Paradise Web Site
- •Adding Code to the Web Forms
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Overview of Mobile Applications
- •The Microsoft Mobile Internet Toolkit
- •Overview of WAP
- •The WAP Architecture
- •Overview of WML
- •The Mobile Web Form
- •The Design of the MobileTimeRetriever Application
- •Creating the Interface for the Mobile Web Forms
- •Adding Code to the MobileTimeRetriever Application
- •Summary
- •Creating the Forms Required for the MobileCallStatus Application
- •Creating the frmLogon Form
- •Creating the frmSelectOption Form
- •Creating the frmPending Form
- •Creating the frmUnattended Form
- •Adding Code to the Submit Button in the frmLogon Form
- •Adding Code to the Query Button in the frmSelectOption Form
- •Adding Code to the Mark checked as complete Button in the frmPending Form
- •Adding Code to the Back Button in the frmPending Form
- •Adding Code to the Accept checked call(s) Button in the frmUnattended Form
- •Adding Code to the Back Button in the frmUnattended Form
- •Summary
- •What Is COM?
- •Windows DNA
- •Microsoft Transaction Server (MTS)
- •.NET Interoperability
- •COM Interoperability
- •Messaging
- •Benefits of Message Queues
- •Limitations
- •Key Messaging Terms
- •Summary
- •Pointers
- •Declaring Pointers
- •Types of Code
- •Implementing Pointers
- •Using Pointers with Managed Code
- •Working with Pointers
- •Compiling Unsafe Code
- •Summary
- •Introduction to the Languages of Visual Studio .NET
- •Visual C# .NET
- •Visual Basic .NET
- •Visual C++ .NET
- •Overview of Visual Basic .NET
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Components of Visual Basic .NET
- •Variables
- •Constants
- •Operators
- •Arrays
- •Collections
- •Procedures
- •Arguments
- •Functions
- •Adding Code to the Submit Button
- •Adding Code to the Exit Button
- •Summary
- •Introduction to Visual Studio .NET IDE
- •Menu Bar
- •Toolbars
- •Visual Studio .NET IDE Windows
- •Toolbox
- •The Task List Window
- •Managing Windows
- •Customizing Visual Studio .NET IDE
- •The Options Dialog Box
- •The Customize Dialog Box
- •Summary
- •Index
176 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
Validation and exception handling form an integral part of any business application. Your application needs to be robust to withstand any anomalies of
application execution.
Anomalies in an application can occur because of unexpected conditions. For example, you may design your application to open a file in the write mode. Although the code will not generate any error if the file is closed, if a user opens the file in another application and then executes your application, your application can generate an unrecoverable error. To avoid such run-time errors, you should implement an exception-handling mechanism in your application.
Exception handling provides many uses in an application. For example, if a user specifies the date in an incorrect format, the user can be allowed to rectify the error and proceed with the registration. Similarly, if the databases pertaining to the application are not responding, the application can display a message to that effect and allow the user to select an alternate location for the database.
This chapter provides an in-depth coverage of the exception-handling capabilities of Visual C#. You will apply exception-handling logic to the Customer Maintenance project. In addition, Visual Studio .NET provides a number of debugging tools that you can use to debug your application.This chapter will also introduce you to the debugging tools and help you use the important ones to debug your application.
Performing Validations
You should always validate data in a Windows form before updating the data in a database. This method has several benefits, some of which include:
Improved response time. The response time of an application is shorter because the application does not need to attempt to update the data in the database and then retrieve an error message because of incorrect data.
Accuracy of data. The application is less prone to sending incorrect data to the database.
VALIDATIONS AND EXCEPTION HANDLING |
Chapter 9 |
|
177 |
|
|||
|
|
|
|
Improved database performance. The load on the database is reduced because it processes optimal transactions only.
In this section, you can learn to validate data for the JobDetails form of the Customer Maintenance project.
Identifying the Validation Mechanism
There are several mechanisms to ensure that only valid values are specified in a user form. Some of these ways are given in the following list:
Selecting the appropriate Windows control for accepting data
Trapping incomplete data when users navigate from one control to another
Validating the form before submitting records to the database
I will now explain each method described here one by one.
Selecting Windows Controls
Often, you can eliminate common errors by using the correct type of controls. For example, instead of using a text box for accepting date values from users, you can use the DateTimePicker control. Similarly, you can use the ListBox control to make the user select an option from a range of options or use the RadioButton control to accept one value from a range of values. In this way, the choices available to the user are limited and the user is less likely to make a mistake.
In the JobDetails form, the value for the JobDate field should be in the date format. Therefore, instead of using a TextBox control, you should use the DateTimePicker control. The steps to add the DateTimePicker control to the form are given in the following list:
1.Open the Customer Maintenance project.
2.Double-click on the JobDetails.cs form to open the code-behind file.
3.Delete the TextBox control from the JobDate field.
4.Drag a DateTimePicker control from the Toolbox to the form.
The changed form, which is obtained after completing the preceding steps, is shown in Figure 9-1.
178 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
FIGURE 9-1 Adding a DateTimePicker control to the for m
|
Y |
L |
|
F |
|
M |
|
A |
|
E |
|
Run the applicationTand open the JobDetails form. You will notice that the current date automatically appears in the form. Similarly, when data is loaded from the database, the JobDate field changes to the one that was specified while adding the record, as shown in Figure 9-2.
FIGURE 9-2 Displaying date and time data from a database
Team-Fly®
VALIDATIONS AND EXCEPTION HANDLING |
Chapter 9 |
|
179 |
|
|||
|
|
|
|
Trapping Incomplete Data
There are certain fields in a database that cannot be left blank when you add records to the database. For example, the CMS (Customer Maintenance System) database uses the tblJobDetails table to store records pertaining to the JobDetails form. Follow these steps to check the fields that are mandatory in the
tblJobDetails table:
1.Open SQL Ser ver Enterprise Manager.
2.In the SQL Server Enterprise Manager window, click on the + (plus) sign next to the name of the SQL server on which the database is installed.
3.In the SQL Server node, expand the CMS database, which stores the
tblJobDetails table.
4.Click on Tables. The tables in the CMS database will appear.
5.Right-click on tblJobDetails and select Properties. The Table Properties - tblJobDetails dialog box will appear. This dialog box shows the fields of the table in which you can have null values, as displayed in Figure 9-3.
FIGURE 9-3 Mandatory fields in a table
180 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
As you can see in Figure 9-3, the CarNo, JobDate, and WorkerId fields are mandatory in the database.The CarNo, JobDate, and WorkerId controls on the JobDetails form represent these fields.
To ensure that a user has specified values for the three fields described above, there are two methods. Either you can validate the required field as soon as a user moves out of it, or you can validate the entire form when the user clicks on the Add or Update button. I examine the procedure to validate an entire form in the next section. In this section, I will describe the ways to validate one field at a time.
When a user selects a control, the Enter event of the control is generated. Similarly, when a user deselects a control, the Leave event of the control is generated. You can use these events to validate controls.
Begin by ensuring that the user has specified a valid car number before the user proceeds to specify the date. When a user tabs out of the CarNo control, the Enter event of the JobDate control and the Leave event of the CarNo control are generated.Therefore, you can check whether the user has specified a valid value for the CarNo either in the Enter event of the JobDate control or in the Leave event of the CarNo control. If the user has not specified a valid value, you can reactivate the CarNo control. To code the functionality, follow these steps:
1.Click on the editCarNo text box in the design view of the JobDetails form.
2.In the Properties window, click on the Events button (the button that has the yellow lightning symbol). All the events available for the TextBox control will appear.
3.From the list of available events, double-click on Leave. The location of this option in the Properties window is shown in Figure 9-4.
4.When you double-click on Leave, Code Editor opens and the event handler for the Leave event is defined. Write the following code for the
Leave event of the editCarNo text box:
private void editCarNo_Leave(object sender, System.EventArgs e)
{
if ((editCarNo.Text==””) || (editCarNo.Text==null))
{
MessageBox.Show(“Please specify a valid value for the car number”,”Error in input”);
VALIDATIONS AND EXCEPTION HANDLING |
Chapter 9 |
181 |
|
|
|
FIGURE 9-4 Adding event handlers for controls
editCarNo.Focus();
}
}
After writing the preceding code, compile and run the application. If you attempt to specify a blank value in the editCarNo text box, the application will display an error message and bring the text box into focus, as shown in Figure 9-5.
FIGURE 9-5 Validating data in controls
182 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
The preceding method has two main drawbacks:
The user cannot decide the order in which controls should be filled. For example, if the user wishes to fill WorkerId before CarNo, the user cannot do so.
The control generates an error message when a user closes the form without specifying a valid value in the CarNo field. This is because the
Leave event of the control is fired even when the user closes the form. Therefore, even if the user decides to discard all changes and close the form, the error message is generated.
To overcome these drawbacks, you can validate data in all the controls when the user clicks on the Update or Add button. This method will be discussed in the next section.
Validating a Form
You can validate data in the JobDetails form in the Click event of the Update button. Validating all the controls simultaneously saves you the effort of coding events for each TextBox control separately. To validate the JobDetails form:
1.Open the JobDetails form in the Design view.
2.Double-click on the Update button. Code Editor opens.
3.Write the following code for the Click event of the JobDetails form:
if (editCarNo.Text.Length <6)
{
MessageBox.Show(“Please specify a valid car Number”); editCarNo.Focus();
return;
}
if (Convert.ToInt32(editWorkerId.Text)<1)
{
MessageBox.Show(“Please specify a valid worker ID”); editWorkerId.Focus();
return;
}