Relationships and Data Integrity |
|
Imagine you are asked to create an application that allows a company to rent cars to potential customers. You may start by creating a list of cars that would be made available to customers. The list may include the type (make, model, and year) of the car and other pieces of information such as options (CD, AC, DVD, etc) that would be of interest to some of the customers. Here is an example of such a list:
When a new customer comes to the store to rent a car, the company clerk would need some of the customer's personal information to keep track of who has the car. The information about the customer may include her name, address, driver's license number, etc. The list of information about the customer may appear as follows:
Once this information is collected, the clerk can present the available cars and their rental rates to the customer so she can select the type of car she wants. The clerk would also be interested to know how long the customer intends to keep the car. After using the car, the customer would bring it back to the store so the car can be made available to other customers. When creating this application, you may be tempted to enter information about the car in the same list that includes the customer's information:
The problem here is that, when a different customer comes to rent the same car, the clerk would have to enter the same pieces of information. Experience shows that when the same information is manually entered over and over again, it could be different from one list to another as human beings are capable of making mistakes. The solution is to create separate lists: one list for the customers, another list for the cars. When a customer comes to rent a car, the clerk can select the customer's information from one list, select the information about the car from another list, and then process a rental order. This technique tremendously reduces the likelihood of making mistakes because information about each item, whether a customer or a car, is created only once and then made available to other lists that would need that information: this is the foundation of relational databases. A relational database is an application in which information flows from one list to another. Microsoft SQL Server, like most database environments, is a relational database system: It allows you to create tables and link them so they can exchange information among themselves.
The transactions among various objects of a database should make sure information of one object is accessible to another object. The objects that hold information, as we have mentioned already, are the tables. To manage the flow of information from one table (A) to another table (B), the table that holds the information, A, must make it available to other tables, such as B. There are two issues that must be dealt with:
To solve the first problem of uniquely identifying records inside of a table, you could create one column or a group of columns used as the primary key. In a relational database, which is the case for most of the databases you will be creating in SQL Server, each table should have at least one primary key. To specify a primary key on a table, as we have already done in the past, you create one column as the PRIMARY KEY constraint and there can be only one PRIMARY KEY constraint on a table. To do this in Enterprise Manager, create a column and specify its data type. Then, on the toolbar, click the Set Primary Key button . To create a primary column using SQL, on the right side of the column definition, type PRIMARY KEY (remember, SQL is case-insensitive). Here is an example: |
CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, Department VARCHAR(50)) GO
Practical Learning: Creating Tables and their Primary Keys |
|
Foreign Keys |
In our introduction to relationships, we illustrated a table that could be used to enter a customer's information and the car he wants to rent:
We also mentioned that it was not effective to put these two categories of information, namely the customer and the car, in the same list. The reason is that the same customer may come at different times to rent different types of cars and the same car is regularly rented by different customers. To reduce the likelihood of mistakes, you should separate these two categories, each in its own list:
This time, if you keep these two lists separate, when it is time to rent a car to a customer, you can use another list that allows the clerk to select the name of the customer, followed by the car she wants to rent:
To make this scenario work, there must be a column in the Rental Order list that represents the customers: this would be the Customer column in our illustration. The column that belongs to the Rental Order list but is used to represent the customers is called a foreign key. This column behaves like an ambassador who is not a citizen of the country where he works but instead represents his native country. Because a foreign key is used to represent a table other than the one where it resides, the table that the foreign key represents must have a primary key that would insure the uniqueness of records in the original table. The table that holds the necessary values and that has the primary key can be referred to as the parent table. In our above illustration, the Customers table is the parent. The table that holds the foreign key is referred to as the child table, which is the case for the Rental Orders list of our illustration. To create a foreign key, you can start by adding the necessary column in the table that will need or use it. There are rules and suggestions you should or must follow. As a suggestion, the name of the column used as the foreign key should be the same as the primary key of the table it represents. As a rule, the data type of the primary key of the parent table must be the same as the data type of the foreign key. If you are working with SQL code, to create a foreign key, when creating the table, before the closing comma of the name of a column (if the column is not the last in the table) or the closing parenthesis of the table (if the column is the last), you can type REFERENCES followed by the name of the parent table with parentheses. In the parentheses of the name of the parent table, enter the name of the primary key column. Here is an example: CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, Department VARCHAR(50)) GO CREATE TABLE StaffMembers ( EmployeeID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, FullName VARCHAR(50), DepartmentID INT REFERENCES Departments(DepartmentID)) GO When you create a table like this, the interpreter would know that, in the StaffMembers table, the DepartmentID column is a foreign key. If you want to explicitly indicate that the column is a foreign key, you can type FOREIGN KEY before specifying it with REFERENCES. Here is an example: CREATE TABLE StaffMembers ( EmployeeID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, FullName VARCHAR(50), DepartmentID INT FOREIGN KEY REFERENCES Departments(DepartmentID)) GO You can also specify the name of the primary key of the parent table in parentheses you would include following FOREIGN KEY. This can be done as follows: CREATE TABLE StaffMembers ( EmployeeID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, FullName VARCHAR(50), DepartmentID INT FOREIGN KEY(DepartmentID) REFERENCES Departments(DepartmentID)) GO If you are planning to reference the foreign key as an object somewhere in your code, you can give it an explicit name. To do this, when creating a table, type CONSTRAINT followed by the name of the foreign key constraint. Here is an example: CREATE TABLE StaffMembers ( EmployeeID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, FullName VARCHAR(50), DepartmentID INT CONSTRAINT FK_Department FOREIGN KEY(DepartmentID) REFERENCES Departments(DepartmentID)) GO If you are working from the SQL Server Enterprise Manager or from the Server Explorer, to specify that a column is used as a foreign key, you can start by creating a column that has the same name and the same data type as the primary key of the parent table. Then, you can right-click anywhere in the table and click Relationships... This would open the Properties dialog box. To actually create a foreign key, in the Properties dialog box, you can click New. You would be asked to select the primary key from the parent table and the foreign from the child table. A name would be generated by the relationship. Once you are satisfied, you can click Close. |
Practical Learning: Creating Foreign Keys |
|
Diagrams |
If you have created the relationships of your tables using SQL code, you only have a vague idea of the flow of information among them. A diagram is a window that visually displays the relationships among tables of a database. A diagram also allows you to troubleshoot a relation when necessary. The Diagram window provides all the tools you need to create, view, change, delete or maintain relationships. To create a diagram, if you are working from the SQL Server Enterprise Manager, in the node of the database, you can right-click the Diagrams node and click New Database Diagram. This would launch the Create Database Diagram Wizard that you can follow to select the necessary tables. Once in the Diagram window, each table displays as a rectangle with dividing lines like a spreadsheet: To effectively work on the tables, you should make sure you can identify each. If the view makes it difficult, you can zoom it. To do this, you can right-click a white area of the window, position the mouse on Zoom, and select a percentage. You can also click the Zoom button on the toolbar. Each table is equipped with a title bar on top that displays its name. If you forgot to add a table, you can right-click a white area in the window and click Add Table... This would display the list of tables of the same database, allowing you to select the desired table(s) before clicking Add and then Close. If you had added a table by mistake or you don't want to include an existing table in the diagram, to remove a table, you can right-click the table and click Remove Table From Diagram. To move a table, you drag its title bar. To create a relationship between two tables, you can right-click anywhere in the diagram and click Relationships... You would then proceed as we did in the section on Foreign Keys. To create or indicate a foreign key, you can drag the necessary column from a parent table to a child table. Once a relationship between two tables exists, there is a line that joins them. You can then manage the relationship. To identify a relationship, you can position the mouse on the line that joins the table: To delete a relationship, you can right-click it and click Delete Relationship From Database. |
Practical Learning: Creating Foreign Keys |
|
Relationships and Data Integrity |
As mentioned in previous sections, relationships allow information to flow from one list, the parent table, to another list, the child table. When maintaining records, sometimes a piece of information may becomes obsolete. An employee may decide to change or to delete it from the parent table. This would cause the relation information in the child table to become orphan. When this happens, you need to take appropriate action. Referential integrity is the ability to take care of necessary details when data from a table gets changed or deleted. When a piece of information is changed in a parent table, you need to make sure that the change is replicated to the related child table. If you are creating or troubleshooting a table using the Design Table from the SQL Server Enterprise Manager or from Server Explorer, after displaying the Create Relationship or the Property Pages, you can click the Cascade Update Related Fields check box. If a piece of information is deleted in a parent, the records of the child table(s) that depended on it should be notified. If you are working from a table in the Design Table from the SQL Server Enterprise Manager or from Server Explorer, after displaying the Create Relationship or the Property Pages, you can click the Cascade Delete Related Records check box.
|
Practical Learning: Insuring Referential Integrity |
Windows Applications and Database Relationships |
When creating an application that uses a database with related tables, to allow the user to select information from parent tables when using forms from child tables, you would configure list-based controls to get their data from the parent tables.
|
Practical Learning: Creating the Windows Application |
|
private void Employees_Load(object sender, System.EventArgs e) { this.sqlDataAdapter1.Fill(this.dsBCR1); } |
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e) { this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnClose_Click(object sender, System.EventArgs e) { Close(); } |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Control | DataBinding | |
Type | Value: dsBCR1 - Customers. | |
txtCustomerID | Text | CustomerID |
txtDrvLicNbr | Text | DrvLicNbr |
txtDLClass | Text | DrvLicClass |
dtpDateIssued | Value | DateIssued |
dtpExpDate | Value | DateExpired |
txtFullName | Text | FullName |
txtAddress | Text | Address |
txtCity | Text | City |
txtState | Text | State |
txtZIPCode | Text | ZIPCode |
txtCountry | Text | Country |
txtHomePhone | Text | HomePhone |
chkOrganDonor | Checked | OrganDonor |
private void btnLoad_Click(object sender, System.EventArgs e) { this.sqlDataAdapter1.Fill(this.dsBCR1); } |
private void btnFirst_Click(object sender, System.EventArgs e) { this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position = 0; this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnPrevious_Click(object sender, System.EventArgs e) { this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position = this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position - 1; this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnNext_Click(object sender, System.EventArgs e) { this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position = this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position + 1; this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnLast_Click(object sender, System.EventArgs e) { this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Position = this.pgeCustomers.BindingContext[this.dsBCR1, "Customers"].Count - 1; this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnClose_Click(object sender, System.EventArgs e) { Close(); } |
private void btnReset_Click(object sender, System.EventArgs e) { this.txtNCDrvLicNbr.Text = ""; this.txtNCDLClass.Text = "C"; this.dtpNCDateIssued.Value = DateTime.Today; this.dtpNCExpDate.Value = DateTime.Today; this.txtNCFullName.Text = ""; this.chkNCOrganDonor.Checked = false; this.txtNCAddress.Text = ""; this.txtNCCity.Text = ""; this.txtNCState.Text = "MD"; this.txtNCZIPCode.Text = ""; this.txtNCCountry.Text = "USA"; this.txtNCHomePhone.Text = "(000) 000-0000"; this.txtNCDrvLicNbr.Focus(); } |
private void btnCreate_Click(object sender, System.EventArgs e) { string strDrvLicNbr = this.txtNCDrvLicNbr.Text; string strFullName = this.txtNCFullName.Text; if( strDrvLicNbr == "" ) { MessageBox.Show("You must provide the customer's driver's license number"); return; } if( strFullName == "" ) { MessageBox.Show("You must provide the customer's name"); return; } string strInsert = "INSERT INTO Customers(DrvLicNbr, DrvLicClass, " + "DateIssued, DateExpired, FullName, Address, " + "City, State, ZIPCode, Country, HomePhone, " + "OrganDonor) VALUES('" + this.txtNCDrvLicNbr.Text + "', '" + txtNCDLClass.Text + "', '" + dtpNCDateIssued.Value + "', '" + dtpNCExpDate.Value + "', '" + txtNCFullName.Text + "', '" + txtNCAddress.Text + "', '" + txtNCCity.Text + "', '" + txtNCState.Text + "', '" + txtNCZIPCode.Text + "', '" + txtNCCountry.Text + "', '" + txtNCHomePhone.Text + "', '" + this.chkNCOrganDonor.Checked + "');"; System.Data.SqlClient.SqlCommand cmdNewCustomer = new System.Data.SqlClient.SqlCommand(strInsert, this.sqlConnection1); sqlConnection1.Open(); cmdNewCustomer.ExecuteNonQuery(); sqlConnection1.Close(); this.btnReset_Click(sender, e); } |
|
private void RentalRates_Load(object sender, System.EventArgs e) { this.sqlDataAdapter1.Fill(this.dsBCR1); } |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Control | DataBindings Type | Selection: dsBCR1-Cars. |
Others |
txtTagNumber | Text | TagNumber | |
txtCarID | Text | CarID | |
txtMake | Text | Make | |
txtModel | Text | Model | |
txtYear | Text | CarYear | |
cboRentalRateID | SelectedValue | RentalRateID | DataSource: dsBCR1.RentalRates DisplayMember: Category ValueMember: RentalRateID |
chkK7Player | Checked | HasK7Player | |
chkDVDPlayer | Checked | HasDVDPlayer | |
chkCDPlayer | Checked | HasCDPlayer | |
chkAvailable | Checked | IsAvailable | |
txtPicture | Text | Picture | |
txtNotes | Text | Notes | |
cboNCRentalRateID | SelectedValue | RentalRateID | DataSource: dsBCR1.RentalRates DisplayMember: Category ValueMember: RentalRateID |
private void btnPicture_Click(object sender, System.EventArgs e) { if( openFileDialog1.ShowDialog() == DialogResult.OK ) { this.txtPicture.Text = openFileDialog1.FileName; this.pctCar.Image = Bitmap.FromFile(openFileDialog1.FileName); } } |
private void btnNCPicture_Click(object sender, System.EventArgs e) { if( openFileDialog1.ShowDialog() == DialogResult.OK ) { this.txtNCPicture.Text = openFileDialog1.FileName; this.pctNCar.Image = Bitmap.FromFile(openFileDialog1.FileName); } } |
private void btnLoad_Click(object sender, System.EventArgs e) { this.sqlDataAdapter2.Fill(this.dsBCR1); this.sqlDataAdapter1.Fill(this.dsBCR1); this.pctCar.Image = Bitmap.FromFile(this.txtPicture.Text); } |
private void btnFirst_Click(object sender, System.EventArgs e) { this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position = 0; this.pctCar.Image = Bitmap.FromFile(this.txtPicture.Text); this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnPrevious_Click(object sender, System.EventArgs e) { this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position = this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position - 1; this.pctCar.Image = Bitmap.FromFile(this.txtPicture.Text); this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnNext_Click(object sender, System.EventArgs e) { this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position = this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position + 1; this.pctCar.Image = Bitmap.FromFile(this.txtPicture.Text); this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnLast_Click(object sender, System.EventArgs e) { this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Position = this.pgeCars.BindingContext[this.dsBCR1, "Cars"].Count - 1; this.pctCar.Image = Bitmap.FromFile(this.txtPicture.Text); this.sqlDataAdapter1.Update(this.dsBCR1); } |
private void btnClose_Click(object sender, System.EventArgs e) { Close(); } |
private void btnReset_Click(object sender, System.EventArgs e) { this.txtNCTagNumber.Text = ""; this.txtNCMake.Text = ""; this.txtNCModel.Text = ""; this.txtNCYear.Text = ""; this.cboNCCategoryID.SelectedIndex = -1; this.chkNCK7Player.Checked = false; this.chkNCDVDPlayer.Checked = false; this.chkNCCDPlayer.Checked = false; this.chkNCAvailable.Checked = false; this.txtNCPicture.Text = "C:\\Programs\\Unknown.bmp"; this.pctNCar.Image = Bitmap.FromFile(this.txtNCPicture.Text); this.txtNCNotes.Text = ""; this.txtNCTagNumber.Focus(); } |
private void btnCreate_Click(object sender, System.EventArgs e) { string strInsert = String.Concat("INSERT INTO Cars(TagNumber, Make, Model, ", "CarYear, RentalRateID, HasK7Player, ", "HasCDPlayer, HasDVDPlayer, Picture, ", "Available, Notes) VALUES('", this.txtNCTagNumber.Text, "', '", txtNCMake.Text, "', '", txtNCModel.Text, "', '", txtNCYear.Text, "', '", cboNCCategoryID.SelectedIndex, "', '", chkNCCDPlayer.Checked, "', '", chkNCDVDPlayer.Checked, "', '", chkNCDVDPlayer.Checked, "', '", txtNCPicture.Text, "', '", chkNCAvailable.Checked, "', '", txtNCNotes.Text, "');"); System.Data.SqlClient.SqlCommand cmdNewCar = new System.Data.SqlClient.SqlCommand(strInsert, this.sqlConnection1); sqlConnection1.Open(); cmdNewCar.ExecuteNonQuery(); sqlConnection1.Close(); this.btnReset_Click(sender, e); } |
|
Control | DataBindings Type | Selection: dsBCR1 -RentalOrders. |
Others |
cboEmployeeID | SelectedValue | EmployeeID | DataSource: dsBCR1.Employees DisplayMember: FullName ValueMember: EmployeeID |
cboCarID | SelectedValue | CarID | DataSource: dsBCR1.Cars DisplayMember: TagNumber ValueMember: CarID |
cboCustomerID | SelectedValue | CustomerID | DataSource: dsBCR1.Customers DisplayMember: DrvLicNbr ValueMember: CustomerID |
private void RentalOrders_Load(object sender, System.EventArgs e) { this.sqlDataAdapter4.Fill(this.dsBCR1); this.sqlDataAdapter3.Fill(this.dsBCR1); this.sqlDataAdapter2.Fill(this.dsBCR1); this.sqlDataAdapter1.Fill(this.dsBCR1); } |
private void cboCarID_SelectedIndexChanged(object sender, System.EventArgs e) { string strTagNumber = "000000"; string strMake = "Not Found"; string strModel = "Not Found"; string strYear = "0"; // Get a reference to the records of the Cars table DataRowCollection colRows = this.dsBCR1.Tables["Cars"].Rows; // Check each record one by one for(int i = 0; i < colRows.Count; i++) { // Get the current tag number strTagNumber = (string)colRows[i]["TagNumber"]; // If the current tag matches the one selected by the user... if( strTagNumber == this.cboCarID.Text ) { // Retrieve its corresponding make, model, and year strMake = (string)colRows[i]["Make"]; strModel = (string)colRows[i]["Model"]; strYear = (string)colRows[i]["CarYear"]; // Since you found a match, STOP!!! break; } } // Display the make, model, and year of the selected car this.txtMake.Text = strMake; this.txtModel.Text = strModel; this.txtCarYear.Text = strYear; } |
private void cboCustomerID_SelectedIndexChanged(object sender, System.EventArgs e) { string strDrvLicNbr = "000000"; string strCustName = "Not Found"; string strCustAddress = ""; string strCustCity = ""; string strCustState = ""; string strCustZIPCode = ""; string strCountry = ""; // Get a reference to the records of the Customers table DataRowCollection colRows = this.dsBCR1.Tables["Customers"].Rows; // Check each record one by one for(int i = 0; i < colRows.Count; i++) { // Get the current tag number strDrvLicNbr = (string)colRows[i]["DrvLicNbr"]; // If the current driver's license number matches the one selected by the user... if( strDrvLicNbr == this.cboCustomerID.Text ) { // Retrieve its corresponding information strCustName = (string)colRows[i]["FullName"]; strCustAddress = (string)colRows[i]["Address"]; strCustCity = (string)colRows[i]["City"]; strCustState = (string)colRows[i]["State"]; strCustZIPCode = (string)colRows[i]["ZIPCode"]; strCountry = (string)colRows[i]["Country"]; // Since you found a match, STOP!!! break; } } // Display the details of the customer this.txtCustName.Text = strCustName; this.txtCustAddress.Text = strCustAddress; this.txtCustCity.Text = strCustCity; this.txtCustState.Text = strCustState; this.txtCustZIPCode.Text = strCustZIPCode; this.txtCustCountry.Text = strCountry; } |
private void btnRateApplied_Click(object sender, System.EventArgs e) { RentalRates frmRates = new RentalRates(); frmRates.Show(); } |
private void btnReset_Click(object sender, System.EventArgs e) { this.cboEmployeeID.SelectedIndex = -1; this.txtRentalOrderID.Text = ""; this.cboCarID.SelectedIndex = -1; this.txtMake.Text = ""; this.txtModel.Text = ""; this.txtCarYear.Text = ""; this.cboCarCondition.SelectedIndex = 0; this.cboCustomerID.SelectedIndex = -1; this.txtCustName.Text = ""; this.txtCustAddress.Text = ""; this.txtCustCity.Text = ""; this.txtCustState.Text = ""; this.txtCustZIPCode.Text = ""; this.txtCustCountry.Text = "USA"; this.cboTankLevel.SelectedIndex = 0; this.txtRateApplied.Text = "24.95"; this.txtMileage.Text = "0"; this.txtSubTotal.Text = "0.00"; this.dtpStartDate.Value = DateTime.Today; this.txtTaxRate.Text = "7.75"; TimeSpan ts = new TimeSpan(1, 0, 0, 0); this.dtpEndDate.Value = DateTime.Today.Add(ts); this.txtTaxAmount.Text = "0.00"; this.txtNumberOfDays.Text = "1"; this.txtOrderTotal.Text = "0.00"; this.cboEmployeeID.Focus(); } |
private void dtpEndDate_ValueChanged(object sender, System.EventArgs e) { DateTime startDate = this.dtpStartDate.Value; DateTime endDate = this.dtpEndDate.Value; TimeSpan diff = endDate.Subtract(startDate); this.txtNumberOfDays.Text = (diff.Days + 1).ToString(); } |
private void txtRateApplied_Leave(object sender, System.EventArgs e) { double rateApplied = double.Parse(this.txtRateApplied.Text); int numberOfDays = int.Parse(this.txtNumberOfDays.Text); double subTotal = rateApplied * numberOfDays; this.txtSubTotal.Text = subTotal.ToString("F"); double taxRate = double.Parse(this.txtTaxRate.Text); double taxAmount = subTotal * taxRate / 100; this.txtTaxAmount.Text = taxAmount.ToString("F"); double orderTotal = subTotal + taxAmount; this.txtOrderTotal.Text = orderTotal.ToString("F"); } |
private void btnNewOrder_Click(object sender, System.EventArgs e) { if( this.btnNewOrder.Text == "New Rental Order" ) { this.btnNewOrder.Text = "Save"; this.btnReset_Click(sender, e); } else { string strInsert = String.Concat("INSERT INTO RentalOrders(", "EmployeeID, CustomerID, CarID, ", "CarCondition, TankLevel, Mileage, ", "StartDate, EndDate, NumberOfDays, ", "RateApplied, SubTotal, TaxRate, TaxAmount, ", "RentTotal, Notes) VALUES('", cboEmployeeID.SelectedIndex.ToString(), "', '", cboCustomerID.SelectedIndex.ToString(), "', '", cboCarID.SelectedIndex.ToString(), "', '", cboCarCondition.Text, "', '", cboTankLevel.Text, "', '", txtMileage.Text, "', '", dtpStartDate.Value, "', '", dtpEndDate.Value, "', '", txtNumberOfDays.Text, "', '", txtRateApplied.Text.ToDecimal(0), "', '", txtSubTotal.Text.ToDecimal(0), "', '", txtTaxRate.Text.ToDecimal(0), "', '", txtTaxAmount.Text.ToDecimal(0), "', '", txtOrderTotal.Text.ToDecimal(0), "', '", txtNotes.Text, "');"); System.Data.SqlClient.SqlCommand cmdNewOrder = new System.Data.SqlClient.SqlCommand(strInsert, this.sqlConnection1); sqlConnection1.Open(); cmdNewOrder.ExecuteNonQuery(); sqlConnection1.Close(); this.btnReset_Click(sender, e); this.btnNewOrder.Text = "New Rental Order"; } } |
|
private void btnRentalOrders_Click(object sender, System.EventArgs e) { RentalOrders frmOrders = new RentalOrders(); frmOrders.Show(); } |
private void btnCars_Click(object sender, System.EventArgs e) { Cars frmCars = new Cars(); frmCars.ShowDialog(); } |
private void btnCustomers_Click(object sender, System.EventArgs e) { Customers frmCust = new Customers(); frmCust.ShowDialog(); } |
private void btnEmployees_Click(object sender, System.EventArgs e) { Employees frmEmpl = new Employees(); frmEmpl.Show(); } |
private void btnClose_Click(object sender, System.EventArgs e) { Close(); } |
private void btnFind_Click(object sender, System.EventArgs e) { string strReceiptNumber = this.txtRentalOrderID.Text; if( strReceiptNumber == "" ) { MessageBox.Show("You must provide a receipt number to look for a rental order"); return; } string strFindOrder = String.Concat("SELECT * FROM RentalOrders WHERE RentalOrderID = '", strReceiptNumber, "'"); System.Data.SqlClient.SqlConnection conDatabase = new System.Data.SqlClient.SqlConnection("Data Source=(local);Database='BCR';Integrated Security=yes"); System.Data.SqlClient.SqlCommand cmdDatabase = new System.Data.SqlClient.SqlCommand(strFindOrder, conDatabase); conDatabase.Open(); System.Data.SqlClient.SqlDataReader rdrRentalOrder; rdrRentalOrder = cmdDatabase.ExecuteReader(); while(rdrRentalOrder.Read()) { this.cboEmployeeID.SelectedIndex = int.Parse((rdrRentalOrder.GetSqlInt32(1) - 1).ToString()); this.cboCustomerID.SelectedIndex = int.Parse((rdrRentalOrder.GetSqlInt32(2) - 1).ToString()); this.cboCarID.SelectedIndex = int.Parse((rdrRentalOrder.GetSqlInt32(3) - 1).ToString()); this.cboCarCondition.Text = rdrRentalOrder.GetString(4); this.cboTankLevel.Text = rdrRentalOrder.GetString(5); this.txtMileage.Text = rdrRentalOrder.GetInt32(6).ToString(); this.dtpStartDate.Value = rdrRentalOrder.GetDateTime(7); this.dtpEndDate.Value = rdrRentalOrder.GetDateTime(8); this.txtNumberOfDays.Text = rdrRentalOrder.GetInt16(9).ToString(); this.txtRateApplied.Text = rdrRentalOrder.GetDecimal(10).ToString(); this.txtSubTotal.Text = rdrRentalOrder.GetDecimal(11).ToString(); this.txtTaxRate.Text = rdrRentalOrder.GetDecimal(12).ToString(); this.txtTaxAmount.Text = rdrRentalOrder.GetDecimal(13).ToString(); this.txtOrderTotal.Text = rdrRentalOrder.GetDecimal(14).ToString(); this.txtNotes.Text = rdrRentalOrder.GetString(15); } rdrRentalOrder.Close(); conDatabase.Close(); } |
|
||
Previous | Copyright © 2005-2016, FunctionX | |
|