- •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
364 Project 3 CREATING A CREATIVE LEARNING PROJECT
Summary
In this chapter, you added functionality to the Creative Learning form. While adding code to the form, you learned about a few new Windows forms controls, such as the ErrorProvider, FileSystemWatcher, NotifyIcon, and ContextMenu controls.
Chapter 17
Interacting with a
Microsoft Word
Document and
Event Viewer
366 Project 3 CREATING A CREATIVE LEARNING PROJECT
In the preceding chapter, you looked at how the Creative Learning application works. However, the way that the Creative Learning application works also involves accessing and processing a Microsoft Word document. In addition, the errors generated during the processing of the Microsoft Word document are trapped in Windows Event Viewer. Therefore, your application needs to interact with both the Microsoft Word document and Windows Event Viewer. This project covers the process of interacting with the Word document and Windows
Event Viewer.
Interacting with a
Microsoft Word Document
In the preceding chapter, you learned how to add a FileSystemWatcher component to your application. This component monitors a specified directory for any changes, such as adding or deleting a file. In addition, you changed the Filter property of the FileSystemWatcher component to *.doc. Doing this restricts the function of a FileSystemWatcher component to only monitoring the Word document. Therefore, when a user adds a Word file to the source directory, the FileSystemWatcher component generates a Created event.
The Created Event
The Created event is a public event of the FileSystemWatcher class.The FileSystemWatcher class generates the Created event when a new file is added to a directory specified in the FullPath property of the event.
To handle the Created event, the FileSystemWatcher class contains a delegate
FileSystemEventHandler. The FileSystemEventHandler delegate takes two para-
meters, the object that causes the event and an argument of the type FileSystemEventArgs that contains information about the event. This information includes the name and path of the directory that is monitored by the FileSystemWatcher component.
INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
367 |
|
|
|
|
|
In this case, each time a new cash memo is added to the source directory, the FileSystemWatcher component raises a Created event. Therefore, to access and process the cash memo, you need to add programming logic to the Created event
of the FileSystemWatcher class.
Adding Code to the Created Event
When a file is added to the source directory, your application needs to process the cash memo file. File processing involves extracting data from the cash memo, such as Cash Memo No. and Total amount payable. This information is then written to an XML document as a chronological summary of the sales recorded at the bookstores of Creative Learning.
Displaying a Notification Icon with a ToolTip in the Status Area
While the application processes one file, you need to disable the FileSystemWatcher component so that the component does not monitor the source directory for that time. In addition, you can change the notification icon in the status area of the taskbar to denote that the application is processing a file. You can also change the text that is displayed when the user points to the notification icon in the status area. You will now add code to the Created event to perform the activities mentioned earlier in the chapter.
watchDir.EnableRaisingEvents=false; icoNotify.Icon=m_Info; icoNotify.Text=”Processed: “+ e.Name;
The preceding code sets the EnableRaisingEvents property of the FileSystemWatcher class to false. Doing this disables the watchDir component for the time the value of the EnableRaisingEvents property is set to true. Next, the Icon property of the NotifyIcon control is changed to display a different icon in the status area. However, before doing this, you need to add the Info.ico file to the bin folder of your application and then create an instance of the Info.ico file by using the following statement:
private System.Drawing.Icon m_Info= new System.Drawing.Icon(“Info.ICO”);
368 Project 3 CREATING A CREATIVE LEARNING PROJECT
Creating an instance of the Info.ico file adds the file to the Icon class of the System.Drawing namespace. When the notification icon is displayed in the status area, you can use the Text property of the notification icon to display a ToolTip. The Text property appends the word Processed: to the name of the file that is being processed. Figure 17-1 displays the notification icon with a ToolTip.
Y
FIGURE 17-1 Notification icon with a ToolTipLin the status area
Extracting Data from a WordFDocument
the Word application, you need to create an instance of the Word.Application-
M A Your application is nowEready to process the Word document. However, to access
Class class. After you are able to access the Word application, you can create an
instance of the Word.DocumentClass class to access the Word document. To do so,
add the following statements to the |
Created event of the FileSystemWatcher |
|
component. |
T |
|
Word.Application wdApp= new Word.ApplicationClass();
Word.Document Doc = new Word.DocumentClass();
After creating the instance of the Word document Doc, you can use the instance to open the document file that the user adds to the source directory. To do this, you can use the Open() method of the Documents class. The Open() method takes 12 parameters. However, only the first parameter, which is an object containing the file name to be opened, is essential. To pass FileName as a parameter to the Open() method, you can create an object, filename, which stores the name of the file and the full path of the directory where the file is stored. To create the filename object, use the following statement:
object filename=e.FullPath;
TIP
The filename object is passed as a reference parameter to the Open() method.
Team-Fly®
INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
369 |
|
|
|
|
|
Except for the first parameter, the rest of the parameters of the Open() method are optional. Therefore, you can create an object of the instance of the Missing class
optional.
NOTE
The Missing class is a public sealed class in the System.Reflection namespace. You cannot inherit a Missing class.
The optional object can now be passed as optional parameters to the Open() method. To do this, use the following statements:
object optional=System.Reflection.Missing.Value;
Doc=wdApp.Documents.Open(ref filename, ref optional, ref optional,ref optional,ref
optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref
optional,ref optional);
The preceding code opens the Word document present in the source directory and stores the entire content of the Word document in the instance of the Word.DocumentClass class Doc. However, in this case, you only require the information in the Cash Memo No. and Total amount payable fields. Figure 17-2 shows a sample cash memo document.
FIGURE 17-2 Sample cash memo document
370 Project 3 CREATING A CREATIVE LEARNING PROJECT
To retrieve the required data from the cash memo document, add the following code to the Created event:
Word.Range wdRange; wdRange=Doc.Paragraphs.Item(2).Range; string strMemo, strAmount;
int intParacount; strMemo=wdRange.Text; strMemo=strMemo.Substring(15,4); intParacount=Doc.Paragraphs.Count; intParacount=intParacount-2;
wdRange=Doc.Paragraphs.Item(intParacount).Range; object count=”-1”;
object wdCharacter=”1”;
wdRange.MoveEnd(ref wdCharacter,ref count); strAmount=wdRange.Text; strAmount=strAmount.Substring(23);
The preceding code creates an object of the type Range, wdRange that stores the content of the Word document. You then use the Item property of the Paragraphs collection to retrieve data from a specified paragraph. As shown in Figure 17-2, Cash Memo No. is the second paragraph in the cash memo document. Therefore, you need to retrieve the content of the second paragraph of the cash memo document by using the Range property. The content that is retrieved is then stored in
wdRange.
The Text property of the wdRange object is used to store the text of the paragraph in a string type variable, strMemo. Until now, the strMemo variable stores the entire content of the second paragraph. However, to just retrieve the value of the Cash Memo No. field, use the Substring() method. The Substring() method takes two parameters, the starting position from where the text is retrieved and the number of characters retrieved.
Similarly, you can store the text of the Total amount payable field in another string type variable, strAmount. The Total amount payable field is the second last paragraph in the cash memo document.Therefore, you need to declare an integer variable, intParacount, that stores the number of paragraphs in a document. You use the Count property of the Paragraphs collection to count the number of paragraphs in the document.