- •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
316 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
//Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text=”Click on an employee code to see their record.”;
}
catch(XmlException e)
{
MessageBox.Show(“XML Exception :”+e.ToString());
}
}
Figure 14-1 displays the TreeView control populated with the employee codes.
FIGURE 14-1 The TreeView control populated with employee codes
Event Handling
An event is the result of an action that has occurred. This action could have occurred as a result of user action, such as a mouse click, or could have been the result of a built-in program logic. For example, when a person rings the doorbell, an event takes place. Another person responds to the event by attending the door. The person ringing the bell is called the event sender and the person responding is the event receiver or handler. However, the person triggering the event is not aware of the person who will be handling the event.
IMPLEMENTING THE BUSINESS LOGIC |
Chapter 14 |
317 |
|
|
|
|
|
To respond to an event, you must provide an event handler method that will handle the events. Suppose you have a simple Windows form that contains a button. When the button is clicked, the event must be handled by an event handler method. The following code shows an event handler.
void Button_Clicked(object sender, EventArgs e)
{
//the program logic
}
However, for the event to be handled, you need to tie up your event handler to an instance of the button. You need to create an instance of EventHandler that takes a reference to Button_Clicked as its argument, as shown in the following code:
button.Click+=new EventHandler(this.Button_Clicked);
This tying up is taken care of by Visual Studio .NET. The following example shows a simple Windows application that handles a button click event.
using System;
using System.ComponentModel; using System.Windows.Forms; using System.Drawing;
public class EventSampleForm: Form
{
private Button button;
public EventSampleForm () : base()
{
button = new Button(); button.Location = new Point(50,100); button.Text = “Click Me”;
// To wire the event, create a delegate instance and add it to the Click event. button.Click += new EventHandler(this.Button_Clicked);
Controls.Add(button);
}
// The event handler.
private void Button_Clicked(object sender, EventArgs e)
318 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
{
MessageBox.Show(“You clicked me!”);
}
//STAThreadAttribute indicates that Windows Forms uses the
//single-threaded apartment model.
[STAThreadAttribute] |
|
|
||
public static void Main(string[] args) |
Y |
|||
{ |
|
|
|
|
|
|
|
|
|
Application.Run(new EventSampleForm ()); |
|
|||
} |
|
|
F |
|
|
|
|
|
|
} |
|
|
M |
|
|
|
|
|
|
The essential steps in an event handlingLapplication are as follows: |
||||
|
|
A |
|
|
The source of an event is an instance of System.Windows.Forms.<control> |
||||
|
control. |
E |
|
|
|
|
T |
|
|
The <control> raises an event. |
|
The delegate for the event is EventHandler.
The form has an event handler called Control_Event.
The Control_Event is tied to the event.
Displaying Employee Details in the ListView Control
In the ERS application, the employee details need to be displayed in the ListView control at the click of an employee code in the TreeView control. Items can be added to ListView control using the ListView Collection Editor or programmatically. For this application, you need to add the items programmatically, because the items are dependent on an event, the click of an employee code node in the TreeView control.
However, before the list view control is populated, you need to create column headers for the ListView control. A column header is an item in a ListView control that contains heading text. I have put the code for displaying the column headers in the initializeListControl method, as given below.
protected void initializeListControl()
{
listView1.Clear();
listView1.Columns.Add(“Employee Name”,225,HorizontalAlignment.Left );
Team-Fly®
IMPLEMENTING THE BUSINESS LOGIC |
Chapter 14 |
319 |
|
|
|
|
|
listView1.Columns.Add(“Date of Join”,70,HorizontalAlignment.Right );
listView1.Columns.Add(“Grade”,105,HorizontalAlignment.Left );
listView1.Columns.Add(“Salary”,105,HorizontalAlignment.Left );
}
The Columns property of the ListView class contains a collection of all the column headers that appear in the control. The Columns property returns a collection containing ColumnHeader objects that are displayed in the ListView control. The ColumnHeader objects define the text to be displayed for a column and is contained
in the ListView.ColumnHeaderCollection.
You can add a column header to the collection using the Add method. Alternatively, you can create an array of ColumnHeader objects and pass it to the AddRange method to add a number of column headers.
Table 14-4 explains some of the commonly used methods of the ListView.Column-
HeaderCollection.
Table 14-4 ListView.ColumnHeaderCollection Members
Method |
Description |
Add |
This overloaded method adds a column header to the collection. |
AddRange |
This method adds an array of column headers to the collection. |
Clear |
This method removes all column headers fr om the collection. |
Contains |
This method determines whether the specified method is contained in the |
|
collection. |
Insert |
This method inserts a column header into the collection at the specified |
|
index. |
Remove |
This method removes the specified column header from the collection. |
RemoveAt |
This method removes the column header at the specified index from within |
|
the collection. |
|
|
The final task is to read the EmpRec.xml XML file and display the details of an employee whose employee code has been clicked in the TreeView control.
protected void PopulateListView(TreeNode currNode)
{
initializeListControl();
320 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
XmlTextReader listRead= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); listRead.MoveToElement();
while(listRead.Read())
{
string strNodename; string strNodePath; string name; string grade; string doj;
string sal;
string[] strItemsArr=new String [4]; listRead.MoveToFirstAttribute(); strNodename=listRead.Value; strNodePath=currNode.FullPath.Remove(0,17); if(strNodePath==strNodename)
{
ListViewItem lvi; listRead.MoveToNextAttribute(); name=listRead.Value; lvi=listView1.Items.Add(name); listRead.Read(); listRead.Read(); listRead.MoveToFirstAttribute(); doj=listRead.Value; lvi.SubItems.Add(doj); listRead.MoveToNextAttribute(); grade=listRead.Value; lvi.SubItems.Add(grade); listRead.MoveToNextAttribute(); sal=listRead.Value; lvi.SubItems.Add(sal); listRead.MoveToNextAttribute(); listRead.MoveToElement(); listRead.ReadString();
}
}
}
IMPLEMENTING THE BUSINESS LOGIC |
Chapter 14 |
321 |
|
|
|
|
|
Figure 14-2 displays the ERS application populated with the employee records.
FIGURE 14-2 The ERS application
The code for the entire application is given here.
using System;
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms; using System.Data;
using System.Xml;
using System.Diagnostics; using System.IO; namespace EmployeeRecords
{
///<summary>
///Summary description for Form1.
///</summary>
public class EmployeeRecordsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1; private System.Windows.Forms.ListView listView1; private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
322Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
///<summary>
///Required designer variable.
///</summary>
private System.ComponentModel.Container components=null; private TreeNode tvRootNode;
public EmployeeRecordsForm()
{
//Required for Windows Form Designer support InitializeComponent();
//TODO: Add any constructor code after InitializeComponent call PopulateTreeView();
initializeListControl();
}
///<summary>
///Clean up any resources being used.
///</summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///<summary>
///Required method for Designer support - do not modify
///the contents of this method with the code editor.
///</summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView(); this.listView1 = new System.Windows.Forms.ListView(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
IMPLEMENTING THE BUSINESS LOGIC |
Chapter 14 |
323 |
|
|
|
|
|
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1))
.BeginInit();
this.SuspendLayout(); this.treeView1.ImageIndex = -1; this.treeView1.Name = “treeView1”; this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(240, 352); this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler (this.treeView1_AfterSelect);
//
// listView1
//
this.listView1.Activation = System.Windows.Forms.ItemActivation.TwoClick; this.listView1.Location = new System.Drawing.Point(240, 0); this.listView1.Name = “listView1”;
this.listView1.Size = new System.Drawing.Size(480, 352); this.listView1.TabIndex = 1;
this.listView1.View = System.Windows.Forms.View.Details;
//
//statusBar1
this.statusBar1.Location = new System.Drawing.Point(0, 357); this.statusBar1.Name = “statusBar1”;
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanel1});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(720, 24); this.statusBar1.TabIndex = 2;
//statusBarPanel1
//
this.statusBarPanel1.Text = “Click the employee code to view details”;
this.statusBarPanel1.Width = 720;
//
// EmployeeRecordsForm
//
324 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(720, 381); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.statusBar1,
this.listView1, this.treeView1}); this.MaximizeBox = false; this.MinimizeBox = false;
this.Name = “EmployeeRecordsForm”;
this.Text = “Employee Records Monitoring System”; ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1))
.EndInit(); this.ResumeLayout(false);
}
#endregion
///<summary>
///The main entry point for the application.
///</summary>
[STAThread] static void Main()
{
Application.Run(new EmployeeRecordsForm());
}
protected void PopulateTreeView()
{
statusBarPanel1.Text=”Refreshing Employee Codes. Please wait...”; this.Cursor = Cursors.WaitCursor;
treeView1.Nodes.Clear();
tvRootNode=new TreeNode(“Employee Records”); this.Cursor = Cursors.Default; treeView1.Nodes.Add(tvRootNode);
TreeNodeCollection nodeCollect = tvRootNode.Nodes; string strVal=””;
XmlTextReader reader= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); reader.MoveToElement();
IMPLEMENTING THE BUSINESS LOGIC |
Chapter 14 |
325 |
|
|
|
|
|
try
{
while(reader.Read())
{
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToElement(); reader.MoveToElement(); reader.MoveToAttribute(“Id”); strVal=reader.Value; reader.Read(); reader.Read(); if(reader.Name==”Dept”)
{
reader.Read();
}
//create the child nodes
TreeNode EcodeNode = new TreeNode(strVal);
//Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text=”Click on an employee code to see their record.”;
}
catch(XmlException e)
{
MessageBox.Show(“XML Exception :”+e.ToString());
}
}
protected void initializeListControl()
{
listView1.Clear();
listView1.Columns.Add(“Employee Name”,225,HorizontalAlignment.Left ); listView1.Columns.Add(“Date of Join”,70,HorizontalAlignment.Right ); listView1.Columns.Add(“Grade”,105,HorizontalAlignment.Left );
326 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT
listView1.Columns.Add(“Salary”,105,HorizontalAlignment.Left );
}
protected void PopulateListView(TreeNode currNode)
{
initializeListControl();
XmlTextReader listRead= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); listRead.MoveToElement();
while(listRead.Read())
{
string strNodename; string strNodePath; string name; string grade; string doj;
string sal;
string[] strItemsArr=new String [4]; listRead.MoveToFirstAttribute(); strNodename=listRead.Value; strNodePath=currNode.FullPath.Remove(0,17); if(strNodePath==strNodename)
{
ListViewItem lvi; listRead.MoveToNextAttribute(); name=listRead.Value; lvi=listView1.Items.Add(name); listRead.Read(); listRead.Read(); listRead.MoveToFirstAttribute(); doj=listRead.Value; lvi.SubItems.Add(doj); listRead.MoveToNextAttribute(); grade=listRead.Value; lvi.SubItems.Add(grade); listRead.MoveToNextAttribute(); sal=listRead.Value; lvi.SubItems.Add(sal);