- •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
350 Project 3 CREATING A CREATIVE LEARNING PROJECT
In the preceding chapter, you looked at the design of the Creative Learning application. This project adds the programming logic to the Creative Learning
application.
Adding the Programming Logic to the Application
Before adding the programming logic to the Creative Learning application, you need to understand how the application works.
1.When the Creative Learning application is run, a user needs to specify the names of the source, destination, and processed file directories.
The application, by default, specifies the names of the source, destination, and processed file directories as D:\Creative\Source,
D:\Creative\Destination, and D:\Creative\Processed. The user may
choose to retain the default directory structure or may change the directory structure as required.
2.When the user adds the names of the specified directories and clicks on the OK button, the application checks whether the entered directory structure is valid.
3.If the user enters an invalid directory structure, the application creates an error message in Error Provider and gives focus to the invalid directory.
4.If the user enters a valid directory structure, the application enables the directory watcher.
5.The application then hides the Creative Learning form and displays a notification icon in the status area of the taskbar.
6.While the application is running, it continuously checks whether the user has added a file to the source directory.
7.When the user adds a file to the source directory, the application disables the directory watcher and changes the notification icon in the status area.
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
351 |
|
|
|
|
|
8.The application then validates the file format, and if the format of the file is not correct, the application generates an error entry in Event Viewer.
9.Alternatively, if the file format is correct, the application processes the file. Processing of the file includes extracting data, such as Cash Memo No. and Total amount payable from the cash memo document.
10.Once the file has been processed, the application saves the information from the cash memo document to an XML document, Summary.XML. This XML document is then saved in the destination directory as specified by the user.
11.After creating the Summary.XML document, the application changes the notification icon again and then enables the directory watcher so that the directory watcher can check the directory for any new file.
You have seen how the application works. You can now code to the application so that the application can be deployed at the client site. To start, add code to the form Load() method.
Adding Code to the Form Load() Method
When the form is loaded by using the form Load() method, the default values of the source, destination, and processed file directory are displayed in txtSource, txtDest, and txtProcessedFile text boxes, respectively. In addition, the optGenerateLog check box is checked by default. When the optGenerateLog check box is checked, any errors that occur while the application is running are logged in the Event Viewer. However, if desired, the user may choose to uncheck the optGenerateLog check box.
To load the form, specify the following code to the Load() method of the Creative Learning form.
private void frmCreative_Load(object sender, System.EventArgs e)
{
txtSource.Text=”D:\\Creative\\Source\\”; txtProcessedFile.Text=”D:\\Creative\\Processed\\”; txtDest.Text=”D:\\Creative\\Destination\\”; optGenerateLog.Checked=true;
}
352 Project 3 CREATING A CREATIVE LEARNING PROJECT
When the application is run, the Creative Learning form looks as shown in Figure 16-1 and Figure 16-2.
FIGURE 16-1 The tabSource page of the Creative Learning form at run time
FIGURE 16-2 The tabDest page of the Creative Learning form at run time
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
353 |
|
|
|
|
|
Adding Code to the OK Button
After entering the required information, such as the names of the source, destination, and processed file directory, the user clicks on the OK button. The application then validates the directory structure that is specified by the user. If the directory structure is found to be incorrect, the application creates an error message in Error Provider and then gives the focus to the invalid directory. However, for it to do so, you first need to add an ErrorProvider control from the Windows Forms toolbox.
The ErrorProvider Control
The ErrorProvider control in Windows forms is used to validate data entered by the user in a control. If the data entered by the user is incorrect, the ErrorProvider control displays an icon adjacent to the control in which the user enters the data. You can change the Icon property of the ErrorProvider control to specify the icon that is displayed when an error occurs. By default, Visual Studio .NET displays the icon as shown in Figure 16-3.
FIGURE 16-3 The default icon of the ErrorProvider control
In addition, the ErrorProvider control displays an error message as a ToolTip when the user points to the icon next to the control. You can specify the error
354 Project 3 CREATING A CREATIVE LEARNING PROJECT
message by using the SetError() method of the ErrorProvider class. You will learn to specify an error message by using the SetError() method later in this chapter.
You can include an ErrorProvider control in the Creative Learning form by performing the following steps:
1.Drag the ErrorProvider control from the Windows Forms toolbox to the form.
Figure 16-4 shows an ErrorProvider control in the Windows Forms toolbox.
The ErrorProvider control gets added to the component tray of the form.
2.Change the Name property of the ErrorProvider control to errMessage.
After adding the ErrorProvider control to the form, you need to associate the ErrorProvider control with the control whose value is to be validated. You can do this by passing the name of the control as a parameter to the SetError() method of the ErrorProvider class. In your project, you need to validate the name of the source, destination, and processed file directories specified in the txtSource, txtDest, and txtProcessedFile text boxes, respectively.
3.To validate the names of the directories entered by the user, add the following code to the Click event of the OK button.
if (!Directory.Exists(txtSource.Text))
{
errMessage.SetError(txtSource,”Invalid source directory”); txtSource.Focus();
tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtSource,””);
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
355 |
|
|
|
|
|
FIGURE 16-4 An ErrorProvider control in the Windows Forms toolbox
The preceding code uses an if loop to validate the directory structure.The if loop uses the Exists() method of the Directory class to check whether the path of the directory specified in the txtSource text box exists on the hard disk. If the path of the directory specified in the txtSource text box does not exist, the ErrorProvider control is used to display an error message.
The SetError() method is used to display an error message. This method takes the name of the control whose value is to be validated, txtSource, and the error message to be displayed as a ToolTip, Invalid source directory, as parameters. The txtSource.Focus(); command is used to set the focus of the application to the txtSource text box.Then, the SelectedTab property of the TabControl control is used to set the tabSource tabbed page as the selected page. However, if the directory structure specified by the user is correct, no text is displayed in the ErrorProvider control. Figure 16-5 shows an icon next to the txtSource text box.
Similarly, you can add code to validate the directory structure in the txtDest and txtProcessedFile text boxes.
356 Project 3 CREATING A CREATIVE LEARNING PROJECT
FIGURE 16-5 An error icon next to the txtSource text box
Another important aspect of the application is when a user specifies an invalid directory structure, the color of the text box changes to pink. To do this, add the following code to the KeyUp event of the txtSource text box.
private void txtSource_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (Directory.Exists(txtSource.Text)) txtSource.BackColor=Color.White;
else txtSource.BackColor=Color.Pink;
}
Similarly, you can add code to the txtDest and txtProcessedFile text boxes.
When the directory structure is validated, you need to enable the directory watcher and display the notification icon in the status area. In addition, you need to hide the Creative Learning form from the taskbar. This will enable your application to appear minimized in the system tray. Before doing that, add a FileS ystemWatcher component to the Creative Learning form.
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
357 |
|
|
|
|
|
The FileSystemWatcher Component
The FileSystemWatcher component in Visual Studio .NET is used to monitor the contents of a directory. This implies that you can monitor the contents of a directory by using the FileSystemWatcher component and perform custom actions when the contents of the directory are changed. For example, you can use the FileSystemWatcher component to find any modifications made to the content of the entire directory or one or more files in the specified directory. When a user makes some change to the files in a specified directory, such as adding, deleting, or modifying a file, the FileS ystemWatcher component generates an event. For example, when a user adds a file to the specified directory, the Created event is generated. You will learn about the Created event in Chapter 17, “Interacting with a Microsoft Word Document and Event Viewer.”
To add a FileSystemWatcher component to the Creative Learning form, perform the following steps:
1.Drag a FileSystemWatcher component from the Components toolbox.
The FileSystemWatcher component gets added to the component tray. Figure 16-6 shows the FileSystemWatcher component in the Components toolbox.
2.Change the following properties of the FileSystemWatcher component.
Name: watchDir
Filter: *.doc
Specifying the value of the Filter property to *.doc restricts the watchDir component to monitoring only Microsoft Word documents.
TIP
By default, the EnableRaisingEvent property of the FileSystemWatcher component is set to true. Therefore, as soon as the FileSystemWatcher component is enabled, it starts monitoring the specified directory and raises an event when a change occurs.
3.To enable the watchDir component, add the following statement to the Click event of the OK button.
watchDir.EnableRaisingEvents=true;
358 Project 3 CREATING A CREATIVE LEARNING PROJECT
Until now, you have not specified the path of the directory that the watchDir component will monitor. In this case, the watchDir component needs to monitor the source directory as entered by the user in the txtSource text box.
4.To specify the path of the directory to the watchDir component, add the following statement to the Click event of the OK button.
watchDir.Path=txtSource.Text; |
|
Y |
|
|
|
||
|
|
L |
|
|
|
F |
|
|
M |
|
|
A |
|
||
E |
|
|
|
T |
|
|
|
FIGURE 16-6 A FileSystemWatcher component in the Components toolbox
After enabling the watchDir component, you need to add a notification icon to the form that is displayed in the status area when the user runs the application. You can do this by adding the NotifyIcon control to your form.
The NotifyIcon Control
The NotifyIcon control in Visual Studio .NET is used to denote a process running in the background as an icon in the status area. For example, when you print a document, the printer icon is displayed in the status area of the taskbar. To specify the icon to be displayed, you need to change the Icons property of the control.
Team-Fly®
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
359 |
|
|
|
|
|
To add the NotifyIcon control to your form, perform the following steps:
1.Drag the NotifyIcon control from the Windows Forms toolbox to the Creative Learning form.
A NotifyIcon control with the name notifyIcon1 gets added to the component tray. Figure 16-7 shows the NotifyIcon control in the Windows Forms toolbox.
2.Change the Name property of the NotifyIcon control to mnuNotify.
To display a notification icon, you need to add the icon file (Ready.ico file) to the bin folder of your application and then create an instance of the icon file.
3.To create an instance of the icon file, add the following statement to the
frmCreative class in the CreativeLearning namespace.
private System.Drawing.Icon m_Ready= new System.Drawing.Icon(“Ready.ICO”);
4.To display the notification icon, add the following code to the Click event of the OK button.
icoNotify.Icon=m_Ready;
icoNotify.Visible=true;
After you have specified the notification icon, the user does not need to see the application. Therefore, you can hide the application from the taskbar.
5.To hide the application, add the following code to the Click event of the OK button.
this.ShowInTaskbar=false;
this.Hide();
The entire code for the OK button is as follows:
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!Directory.Exists(txtSource.Text))
{
errMessage.SetError(txtSource,”Invalid source directory”);
360 Project 3 CREATING A CREATIVE LEARNING PROJECT
FIGURE 16-7 The NotifyIcon control in the Windows Forms toolbox
txtSource.Focus(); tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtSource,””);
if (!Directory.Exists(txtDest.Text))
{
errMessage.SetError(txtDest,”Invalid destination directory”); txtDest.Focus();
tabControl1.SelectedTab=tabDest; return;
}
else errMessage.SetError(txtDest,””);
if (!Directory.Exists(txtProcessedFile.Text))
{
errMessage.SetError(txtProcessedFile,”Invalid processed file directory”); txtProcessedFile.Focus();
IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
361 |
|
|
|
|
|
tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtProcessedFile,””); watchDir.Path=txtSource.Text; watchDir.EnableRaisingEvents=true; icoNotify.Icon=m_Ready; icoNotify.Visible=true; this.ShowInTaskbar=false;
this.Hide();
}
Visual Studio .NET also allows you to add a context menu to the NotifyIcon control.The context menu is displayed when the user right-clicks on the notification icon in the status area of the taskbar.
The ContextMenu Control
The ContextMenu control in Visual Studio .NET allows you to create a menu that consists of frequently used commands. The menu that is created is called a context menu. A context menu is associated with a control in a Windows form. The user can access the context menu by right-clicking on the control in the Windows form.
To add a ContextMenu control to the NotifyIcon control, perform the following steps:
1.Drag a ContextMenu control from the Windows Forms control to the Creative Learning form.
A ContextMenu control with the name contextMenu1 is added to the component tray. In addition, a context menu is added to the top of the form.
2.Click on the text Context Menu on the top of the form to add menus to the ContextMenu control.
A menu item gets added to the ContextMenu control. You can change the text of the menu item by typing the text in the Type Here area.
3.Type the name of the menu item as Configure Application.
362Project 3 CREATING A CREATIVE LEARNING PROJECT
4.Change the Name property of the menu item to mnuConfigure.
To add another menu item to the ContextMenu control, type the name of the menu item in the Type Here area below the Configure Application menu item.
5.Type the name of the second menu item as Exit.
6.Change the Name property of the menu item to mnuExit.
Figure 16-8 displays the ContextMenu control in the design view.
FIGURE 16-8 The ContextMenu control in the design vie w
However, the menu items that you created do not contain the code so far.
7.To make the mnuConfigure menu item functional, add the following code to the Click event of the mnuConfigure menu item.
private void mnuConfigure_Click(object sender, System.EventArgs e)
{
icoNotify.Visible=false; this.ShowInTaskbar=true; this.Show();
}