
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
218 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
else
{
currentPosition = this.BindingContext[customerDataSet1, “tblCustomer”].Position + 1;
txtDisplayPosition.Text = currentPosition.ToString() + “ of “ + ctr.ToString() ;
}
}
The previous code declares two integer variables, currentPosition and ctr. The |
||
|
|
Y |
Count property of the BindingContext object is used to find the number of records |
||
|
L |
|
in customerDataSet. The value returned by the Count property is stored in the vari- |
||
able ctr. |
F |
|
|
M |
|
The if construct is used to check the value of ctr. If the value of ctr is equal to |
zero, it implies that there are no records in the table. However, if the value of ctr is not equal to zero, the value of the current record is converted to a string by using the ToString() method and is then displayed in the txtDisplayPosition text box.
A E Similarly, you canTwrite the code for the Next button. The Next button enables
the user to navigate to the next record in the table. To make the Next button functional, add the following code to the Click event of the Next button:
private void btnNext_Click(object sender, System.EventArgs e)
{
btnNext.BindingContext[customerDataSet1, “tblCustomer”].Position +=1 ; CurrentPosition();
}
Figure 10-6 shows CustomerForm.
FIGURE 10-6 CustomerForm
Team-Fly®

DATABASE INTERACTION USING ADO.NET |
Chapter 10 |
219 |
|
|
|
|
|
After adding the functionality for all the controls, the complete code for the CustomerForm form is as follows:
using System;
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms;
namespace Customer_Maintenance_Project
{
public class CustomerForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnEdit; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Label lblCarNo; private System.Windows.Forms.Label lblName; private System.Windows.Forms.Label lblAddress; private System.Windows.Forms.Label lblMake; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.SqlClient.SqlCommand sqlUpdateCommand1; private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; private System.Data.SqlClient.SqlConnection sqlConnection1;
private Customer_Maintenance_Project.CustomerDataSet customerDataSet1; private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Button Exit;
private System.Windows.Forms.TextBox txtDisplayPosition; private System.Windows.Forms.Button btnBack;
private System.ComponentModel.Container components = null; public CustomerForm()

220 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void btnEdit_Click(object sender, System.EventArgs e)
{
customerDataSet1.Clear(); sqlDataAdapter1.Fill(customerDataSet1); CurrentPosition();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
bool flag; flag=true;
if (textBox1.Text==””)
{
errCustForm.SetError(textBox1,”Please specify a valid car number.”); flag=false;
}
else errCustForm.SetError(textBox1,””);
if (textBox2.Text==””)
{
errCustForm.SetError(textBox2,”Please specify a valid name.”); flag=false;
}

DATABASE INTERACTION USING ADO.NET |
Chapter 10 |
221 |
|
|
|
|
|
else errCustForm.SetError(textBox2,””);
if (textBox3.Text==””)
{
errCustForm.SetError(textBox3,”Please specify a valid address.”); flag=false;
}
else errCustForm.SetError(textBox3,””);
if (textBox4.Text==””)
{
errCustForm.SetError(textBox4,”Please specify a valid make.”); flag=false;
}
else errCustForm.SetError(textBox4,””);
if (flag==false) return;
else
{
sqlDataAdapter1.Update(customerDataSet1); MessageBox.Show(“Database updated!”);
}
}
private void btnBack_Click(object sender, System.EventArgs e)
{
btnBack.BindingContext[customerDataSet1, “tblCustomer”].Position -=1 ; CurrentPosition();
}
private void btnNext_Click(object sender, System.EventArgs e)
{
btnNext.BindingContext[customerDataSet1, “tblCustomer”].Position +=1 ; CurrentPosition();
}

222 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
private void CustomerForm_Load(object sender, System.EventArgs e)
{
errCustForm.SetError(textBox1,””); errCustForm.SetError(textBox2,””); errCustForm.SetError(textBox3,””); errCustForm.SetError(textBox4,””);
}
private void CurrentPosition()
{
int currentPosition, ctr;
ctr = this.BindingContext[customerDataSet1, “tblCustomer”].Count; if(ctr == 0)
{
txtDisplayPosition.Text = “(There are no records in the Customer table.)”;
}
else
{
currentPosition = this.BindingContext[customerDataSet1, “tblCustomer”].Position + 1;
txtDisplayPosition.Text = currentPosition.ToString() + “ of “ + ctr.ToString() ;
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
}
private void Exit_Click(object sender, System.EventArgs e)
{
Form1 newForm1 = new Form1(); newForm1.Show();
this.Hide();
}

DATABASE INTERACTION USING ADO.NET |
Chapter 10 |
|
223 |
|
|
||||
|
|
|
|
|
private void btnBack_Click_1(object sender, System.EventArgs e)
{
btnBack.BindingContext[customerDataSet1, “tblCustomer”].Position -=1 ; CurrentPosition();
}
}
}
Connecting the JobDetails Form
to the tblJobDetails Table
The JobDetails form is used to display records from the tblJobDetails table. Visual Studio .NET provides us with the Data Form Wizard that you can use to generate datasets, add data-bound controls, and add functionality to the controls. Perform the following steps to run the Data Form Wizard.
1.Right-click on Customer Maintenance Project in the Solution Explorer window.
2.From the list that is displayed, point to the Add option and click on the Add New Item option.
The Add New Item dialog box is displayed.
3.From the Templates: pane, select the Data Form Wizard icon.
4.In the Name text box, type the name as JobDetails.
5.Click on the Open button to close the Add New Item dialog box. The Data Form Wizard is displayed.
6.Click on the Next button to start the Wizard.
The Choose the dataset you want to use page is displayed.
7.Select the Create a new dataset named: radio button to create a new dataset.
8.Type the name of the dataset as JobDataSet in the text box.

224Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
9.Click on the Next button.
The Choose a data connection page is displayed.
10.From the Which connection should the wizard use? list box, choose the name of the database, CMS.
You can also create a new connection by using the New Connection button.
11.Click on the Next button.
The Choose tables or views page is displayed.
12.Add the tables from the list by clicking on the Right Arrow button. You can select multiple tables from the available list.
13.Click on the Next button.
The Choose tables and columns on the form page is displayed.
14.Select the fields that you want to display on the form.
By default, all the fields are selected. If you do not want to display a field, deselect the field name.
15.Click on the Next button.
The Choose the display style page is displayed. This page provides you with the option of creating a DataGrid control or individual controls.
16.Select the Single record in individual controls radio button.
The Add, Delete, Cancel, and Navigation controls check box becomes active. If you do not want a button to appear on the form, you can uncheck the corresponding check box.
17.Click on the Finish button to close the wizard.
Figure 10-7 displays the JobDetails form as created by the wizard.

DATABASE INTERACTION USING ADO.NET |
Chapter 10 |
225 |
|
|
|
|
|
FIGURE 10-7 The JobDetails form
As you can see, the Data Form Wizard creates the data-bound controls and buttons for you. You can now change the layout of the form and add an Exit button. When a user clicks the Exit button, Form1 is displayed. In addition, the JobDetails form is hidden. The code for the JobDetails form is as follows:
using System;
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms;
namespace Customer_Maintenance_Project
{
public class JobDetails : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; private Customer_Maintenance_Project.JobDataSet objJobDataSet; private System.Data.OleDb.OleDbConnection oleDbConnection1;

226 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnUpdate; private System.Windows.Forms.Button btnCancelAll; private System.Windows.Forms.Label lblCarNo; private System.Windows.Forms.Label lblJobDate; private System.Windows.Forms.Label lblWorkerId; private System.Windows.Forms.Label lblKMs; private System.Windows.Forms.Label lblTuning; private System.Windows.Forms.Label lblAlignment; private System.Windows.Forms.Label lblBalancing; private System.Windows.Forms.LabellblTires; private System.Windows.Forms.Label lblWeights; private System.Windows.Forms.Label lblOilChanged; private System.Windows.Forms.Label lblOilQty; private System.Windows.Forms.TextBox editCarNo; private System.Windows.Forms.TextBox editJobDate; private System.Windows.Forms.TextBox editWorkerId; private System.Windows.Forms.TextBox editKMs; private System.Windows.Forms.TextBox editTuning;
private System.Windows.Forms.TextBox editAlignment; private System.Windows.Forms.TextBox editBalancing; private System.Windows.Forms.TextBox editTires; private System.Windows.Forms.TextBox editWeights; private System.Windows.Forms.TextBox editOilChanged; private System.Windows.Forms.TextBox editOilQty; private System.Windows.Forms.Label lblOilFilter; private System.Windows.Forms.Label lblGearOil; private System.Windows.Forms.Label lblGearOilQty; private System.Windows.Forms.Label lblPoint; private System.Windows.Forms.Label lblCondenser; private System.Windows.Forms.Label lblPlug;
private System.Windows.Forms.Label lblPlugQty; private System.Windows.Forms.Label lblFuelFilter; private System.Windows.Forms.Label lblAirFilter; private System.Windows.Forms.Label lblRemarks; private System.Windows.Forms.TextBox editOilFilter;

DATABASE INTERACTION USING ADO.NET |
Chapter 10 |
227 |
|
|
|
|
|
private System.Windows.Forms.TextBox editGearOil; private System.Windows.Forms.TextBox editGearOilQty; private System.Windows.Forms.TextBox editPoint; private System.Windows.Forms.TextBox editCondenser; private System.Windows.Forms.TextBox editPlug; private System.Windows.Forms.TextBox editPlugQty; private System.Windows.Forms.TextBox editFuelFilter; private System.Windows.Forms.TextBox editAirFilter; private System.Windows.Forms.TextBox editRemarks; private System.Windows.Forms.Button btnNavFirst; private System.Windows.Forms.Button btnNavPrev; private System.Windows.Forms.Label lblNavLocation; private System.Windows.Forms.Button btnNavNext; private System.Windows.Forms.Button btnLast; private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnExit;
private System.ComponentModel.Container components = null;
public JobDetails()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}