You are on page 1of 16

Appendix

A
Minor Project 1: Project Planner

The objective of the Project Planner application is to facilitate project and employee management in an organization. It is a form-based Windows application that uses the TabControl for segregating the project management and employee management activities. The main form of the Project Planner application has two tabs: Employee and Project.

Form1.cs [Design] The following gures show the Employee and Project tabs in the Design view:

The Employee Tab in Design View

440

Programming in C#

The project tab in design view

Backend Database As shown in the above gures, the Project Planner application uses a number of elds for recording employee and project related information. Thus, a backend database must be created for recording this information. The Project Planner application uses Microsoft Access as the backend database. The following gures show a snapshot of tables, Table 1 and Table 2, created in MS Access for storing Employee and Project related information: Linking Data Sources As we can see in the Design view, both Employee and Project tabs use the DataGridView control for displaying the records submitted by the user. To populate the DataGridView from a backend data source, it must be linked with the corresponding data source. The following are the steps for linking DataGridView control with a data source: 1. Select the DataGridView control and open the DataGridView Tasks pane, as shown below: 2. Select the Add Project Data Source option from Choose Data Source list to initiate the Data Source Conguration Wizard, as shown below: 3. Select the Database option and click Next to display the Choose Your Data Connection screen, as shown below: 4. Click the New Connection button to display the Add Connection dialog box, as shown below:

Appendix A:

Minor Project 1: Project Planner

441

MS access tables

DataGridView tasks pane

442

Programming in C#

The Data source conguration wizard

The choose Your Data connection screen

Appendix A:

Minor Project 1: Project Planner

443

The add Connection Dialog Box

5.

Select the Microsoft Access Database File (OLE DB) option in the Data Source eld, enter the location of the backend MS Access le in the Database le name text box and click OK. A message box appears conrming the successful data source connection, as shown below:

Microsoft Visual Studio Message Box

6.

Click Next to display the Save the Connection String to the Application Conguration le screen, as shown below:

444

Programming in C#

The Save the Connection String to the Application Conguration le screen

7.

Click Next to display the Choose Your Database Objects screen, as shown below:

The Choose Your Database Objects Screen

Appendix A:

Minor Project 1: Project Planner

445

8.

Select the table and the corresponding columns that you want to display in the DataGridView and click Finish to link the selected data source with the DataGridView control.

Form1.cs Code File The following is the source code contained in the Form1.cs le:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.textBox1.Visible = false; this.textBox2.Visible = false; this.comboBox1.Visible = false; this.comboBox2.Visible = false; this.comboBox5.Visible = false; this.label1.Visible = false; this.label2.Visible = false; this.label3.Visible = false; this.label4.Visible = false; this.label7.Visible = false; this.button4.Visible = false; this.dataGridView1.Visible = false; this.button6.Visible = false; this.label8.Visible = false; this.label9.Visible = false; this.label10.Visible = false; this.label11.Visible = false; this.label12.Visible = false; this.textBox3.Visible = false; this.textBox4.Visible = false; this.textBox5.Visible = false; this.dateTimePicker1.Visible = false; this.dateTimePicker2.Visible = false; this.dataGridView2.Visible = false; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void tabPage1_Click(object sender, EventArgs e) {

446

Programming in C#

} private void button4_Click(object sender, EventArgs e) { DataRow row = dBDataSet.Table1.NewRow(); row[Name]= this.textBox1.Text; row[Dept] = this.comboBox1.Text; row[Exp] = this.comboBox2.Text; row[Team] = this.comboBox5.Text; this.dBDataSet.Table1.Rows.Add(row); string connection = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Minor App\\Project Planner\\DB.mdb; string query = SELECT * FROM Table1; OleDbDataAdapter DA = new OleDbDataAdapter(query, connection); OleDbCommandBuilder CMD = new OleDbCommandBuilder(DA); DA.Update(dBDataSet.Table1); this.dBDataSet.Table1.Clear(); MessageBox.Show(Employee Added Successfully); this.textBox1.Visible = false; this.textBox2.Visible = false; this.comboBox1.Visible = false; this.comboBox2.Visible = false; this.comboBox5.Visible = false; this.label1.Visible = false; this.label2.Visible = false; this.label3.Visible = false; this.label4.Visible = false; this.label7.Visible = false; this.button4.Visible = false; } private void button1_Click(object sender, EventArgs e) { this.dataGridView1.Visible = false; this.textBox1.Visible = true; this.textBox2.Visible = true; this.comboBox1.Visible = true; this.comboBox2.Visible = true; this.comboBox5.Visible = true; this.label1.Visible = true; this.label2.Visible = true; this.label3.Visible = true; this.label4.Visible = true; this.label7.Visible = true; this.button4.Visible = true; this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; this.comboBox5.SelectedIndex = 0; } private void Form1_Load(object sender, EventArgs e) { }

Appendix A:

Minor Project 1: Project Planner

447

private void llByToolStripButton_Click(object sender, EventArgs e) { try { this.table1TableAdapter.FillBy(this.dBDataSet.Table1); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } try { this.table2TableAdapter.FillBy(this.dBDataSet1.Table2); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } private void button3_Click(object sender, EventArgs e) { string connection = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Minor App\\Project Planner\\DB.mdb; string query = SELECT * FROM Table1; OleDbDataAdapter DA = new OleDbDataAdapter(query, connection); OleDbCommandBuilder CMD = new OleDbCommandBuilder(DA); DA.Fill(dBDataSet.Table1); BindingSource BindSource = new BindingSource(); BindSource.DataSource = this.dBDataSet.Table1; this.dataGridView1.DataSource = BindSource; this.dataGridView1.Visible = true; } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void tabPage2_Click(object sender, EventArgs e) { } private void label11_Click(object sender, EventArgs e) { } private void button5_Click(object sender, EventArgs e) { this.button6.Visible = true; this.label8.Visible = true; this.label9.Visible = true; this.label10.Visible = true; this.label11.Visible = true;

448

Programming in C#

this.label12.Visible = true; this.textBox3.Visible = true; this.textBox4.Visible = true; this.textBox5.Visible = true; this.dateTimePicker1.Visible = true; this.dateTimePicker2.Visible = true; } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { } private void button7_Click(object sender, EventArgs e) { string connection = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Minor App\\Project Planner\\DB.mdb; string query = SELECT * FROM Table2; OleDbDataAdapter DA = new OleDbDataAdapter(query, connection); OleDbCommandBuilder CMD = new OleDbCommandBuilder(DA); DA.Fill(dBDataSet1.Table2); BindingSource BindSource = new BindingSource(); BindSource.DataSource = this.dBDataSet1.Table2; this.dataGridView2.DataSource = BindSource; this.dataGridView2.Visible = true; } private void button6_Click(object sender, EventArgs e) { this.dBDataSet1.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema; DataRow row = dBDataSet1.Table2.NewRow(); row[Name] = this.textBox3.Text; row[Variant] = this.comboBox4.Text; row[StartDate] = this.dateTimePicker1.Value; row[EndDate] = this.dateTimePicker2.Value; row[ManMonths] = this.textBox5.Text; this.dBDataSet1.Table2.Rows.Add(row); string connection1 = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Minor App\\Project Planner\\DB.mdb; string query = SELECT * FROM Table2; OleDbDataAdapter DA1 = new OleDbDataAdapter(query, connection1); OleDbCommandBuilder CMD = new OleDbCommandBuilder(DA1); DA1.Update(dBDataSet1.Table2); this.dBDataSet1.Table2.Clear(); MessageBox.Show(Project Added Successfully); this.button6.Visible = false; this.label8.Visible = false; this.label9.Visible = false; this.label10.Visible = false; this.label11.Visible = false; this.label12.Visible = false;

Appendix A:

Minor Project 1: Project Planner

449

this.textBox3.Visible = false; this.textBox4.Visible = false; this.textBox5.Visible = false; this.dateTimePicker1.Visible = false; this.dateTimePicker2.Visible = false; } private void textBox3_TextChanged(object sender, EventArgs e) { } private void llByToolStripButton_Click_1(object sender, EventArgs e) { try { this.table2TableAdapter.FillBy(this.dBDataSet1.Table2); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } private void llByToolStripButton_Click_2(object sender, EventArgs e) { try { this.table2TableAdapter.FillBy(this.dBDataSet1.Table2); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } } }

Adding INSERT Query The above code uses the Update method for posting records into the database. Thus, an appropriate SQL Insert query is required to be created, as shown in the following steps: 1. Open the DBDataSet.xsd le and select Data Add Query to initiate the TableAdapter Query Conguration Wizard, as shown below: 2. Click Next to display the Choose a Command Type screen, as shown below: 3. Click Next to display the Choose a Query Type screen, as shown below: 4. Select the INSERT option and click Next. The SQL INSERT query will automatically get created. Click Finish to close the TableAdapter Query Conguration Wizard. This completes the development of the Project Planner application. Running the Project Planner Application The following gures show the output of the Project Planner application and how it is used for employee and project management:

450

Programming in C#

The TableAdapter Query Conguration Wizard

The Choose a Command Type Screen

Appendix A:

Minor Project 1: Project Planner

451

The Choose a Query Type Screen

Output of Project Planner Application

452

Programming in C#

Adding New Employee Records

Viewing Added Records

Appendix A:

Minor Project 1: Project Planner

453

The Project Tab

Adding New Project Details

454

Programming in C#

Viewing Project Details

You might also like