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, in Lesson 15, we saw that 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 Sub Employees_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load SqlDataAdapter1.Fill(DsBCR1) End Sub |
Private Sub dataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles dataGrid1.CurrentCellChanged SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click Close() End Sub |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click SqlDataAdapter1.Fill(DsBCR1) End Sub |
Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click pgeCustomers.BindingContext(DsBCR1, "Customers").Position = 0 SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click pgeCustomers.BindingContext(DsBCR1, "Customers").Position = _ pgeCustomers.BindingContext(DsBCR1, "Customers").Position - 1 SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click pgeCustomers.BindingContext(DsBCR1, "Customers").Position = _ pgeCustomers.BindingContext(DsBCR1, "Customers").Position + 1 SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click pgeCustomers.BindingContext(DsBCR1, "Customers").Position = _ pgeCustomers.BindingContext(DsBCR1, "Customers").Count - 1 SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click Close() End Sub |
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) txtNCDrvLicNbr.Text = "" txtNCDLClass.Text = "C" dtpNCDateIssued.Value = DateTime.Today dtpNCExpDate.Value = DateTime.Today txtNCFullName.Text = "" chkNCOrganDonor.Checked = False txtNCAddress.Text = "" txtNCCity.Text = "" txtNCState.Text = "MD" txtNCZIPCode.Text = "" txtNCCountry.Text = "USA" txtNCHomePhone.Text = "(000) 000-0000" txtNCDrvLicNbr.Focus() End Sub |
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click Dim strDrvLicNbr As String = txtNCDrvLicNbr.Text Dim strFullName As String = txtNCFullName.Text If strDrvLicNbr = "" Then MsgBox("You must provide the customer's driver's license number") Exit Sub End If If strFullName = "" Then MsgBox("You must provide the customer's name") Exit Sub End If Dim strInsert As String = "INSERT INTO Customers(DrvLicNbr, DrvLicClass, " & _ "DateIssued, DateExpired, FullName, Addre& _ss, " & _ "City, State, ZIPCode, Country, HomePhone, " & _ "OrganDonor) VALUES('" & _ txtNCDrvLicNbr.Text & "', '" & txtNCDLClass.Text & _ "', '" & dtpNCDateIssued.Value & "', '" & _ dtpNCExpDate.Value & "', '" & txtNCFullName.Text & _ "', '" & txtNCAddress.Text & "', '" & _ txtNCCity.Text & "', '" & txtNCState.Text & "', '" & _ txtNCZIPCode.Text & "', '" & txtNCCountry.Text & "', '" & _ txtNCHomePhone.Text & "', '" & chkNCOrganDonor.Checked & "')" Dim cmdNewCustomer As System.Data.SqlClient.SqlCommand = _ New System.Data.SqlClient.SqlCommand(strInsert, SqlConnection1) SqlConnection1.Open() cmdNewCustomer.ExecuteNonQuery() SqlConnection1.Close() btnReset_Click(sender, e) End Sub |
|
Private Sub RentalRates_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DsBCR1) End Sub |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 Sub btnPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPicture.Click If OpenFileDialog1.ShowDialog() = DialogResult.OK Then txtPicture.Text = OpenFileDialog1.FileName pctCar.Image = Bitmap.FromFile(OpenFileDialog1.FileName) End If End Sub |
Private Sub btnNCPicture_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNCPicture.Click If OpenFileDialog1.ShowDialog() = DialogResult.OK Then txtNCPicture.Text = OpenFileDialog1.FileName pctNCar.Image = Bitmap.FromFile(OpenFileDialog1.FileName) End If End Sub |
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click SqlDataAdapter2.Fill(DsBCR1) SqlDataAdapter1.Fill(DsBCR1) pctCar.Image = Bitmap.FromFile(txtPicture.Text) End Sub |
Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click pgeCars.BindingContext(DsBCR1, "Cars").Position = 0 pctCar.Image = Bitmap.FromFile(txtPicture.Text) SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click pgeCars.BindingContext(DsBCR1, "Cars").Position = _ pgeCars.BindingContext(DsBCR1, "Cars").Position - 1 pctCar.Image = Bitmap.FromFile(txtPicture.Text) SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click pgeCars.BindingContext(DsBCR1, "Cars").Position = _ pgeCars.BindingContext(DsBCR1, "Cars").Position + 1 pctCar.Image = Bitmap.FromFile(txtPicture.Text) SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click pgeCars.BindingContext(DsBCR1, "Cars").Position = _ pgeCars.BindingContext(DsBCR1, "Cars").Count - 1 pctCar.Image = Bitmap.FromFile(txtPicture.Text) SqlDataAdapter1.Update(DsBCR1) End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click Close() End Sub |
Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click txtNCTagNumber.Text = "" txtNCMake.Text = "" txtNCModel.Text = "" txtNCYear.Text = "" cboNCRentalRateID.SelectedIndex = -1 chkNCK7Player.Checked = False chkNCDVDPlayer.Checked = False chkNCCDPlayer.Checked = False chkNCAvailable.Checked = False txtNCPicture.Text = "C:\\Programs\\Unknown.bmp" pctNCar.Image = Bitmap.FromFile(txtNCPicture.Text) txtNCNotes.Text = "" txtNCTagNumber.Focus() End Sub |
Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click Dim strInsert As String = "INSERT INTO Cars(TagNumber, Make, Model, " & _ "CarYear, RentalRateID, HasK7Player, " & _ "HasCDPlayer, HasDVDPlayer, Picture, " & _ "Available, Notes) VALUES('" & _ txtNCTagNumber.Text & "', '" & _ txtNCMake.Text & "', '" & txtNCModel.Text & _ "', '" & txtNCYear.Text & "', '" & _ cboNCRentalRateID.SelectedIndex & "', '" & _ chkNCCDPlayer.Checked & "', '" & _ chkNCDVDPlayer.Checked & "', '" & _ chkNCDVDPlayer.Checked & "', '" & _ txtNCPicture.Text & "', '" & _ chkNCAvailable.Checked & "', '" & _ txtNCNotes.Text & "')" Dim cmdNewCar As System.Data.SqlClient.SqlCommand = New _ System.Data.SqlClient.SqlCommand(strInsert, SqlConnection1) SqlConnection1.Open() cmdNewCar.ExecuteNonQuery() SqlConnection1.Close() btnReset_Click(sender, e) End Sub |
|
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 Sub RentalOrders_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter4.Fill(DsBCR1) SqlDataAdapter3.Fill(DsBCR1) SqlDataAdapter2.Fill(DsBCR1) SqlDataAdapter1.Fill(DsBCR1) End Sub |
Private Sub cboCarID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCarID.SelectedIndexChanged Dim strTagNumber As String = "000000" Dim strMake As String = "Not Found" Dim strModel As String = "Not Found" Dim strYear As String = "0" Dim i As Integer ' Get a reference to the records of the Cars table Dim rows As DataRowCollection = DsBCR1.Tables("Cars").Rows ' Check each record one by one For i = 0 To rows.Count - 1 Step 1 ' Get the current tag number strTagNumber = CStr(rows.Item(i).Item("TagNumber")) ' If the current tag matches the one selected by the user... If strTagNumber = cboCarID.Text Then ' Retrieve its corresponding make, model, and year strMake = CStr(rows.Item(i).Item("Make")) strModel = CStr(rows.Item(i).Item("Model")) strYear = CStr(rows.Item(i).Item("CarYear")) End If Next ' Display the make, model, and year of the selected car txtMake.Text = strMake txtModel.Text = strModel txtCarYear.Text = strYear End Sub |
Private Sub cboCustomerID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCustomerID.SelectedIndexChanged Dim strDrvLicNbr As String = "000000" Dim strCustName As String = "Not Found" Dim strCustAddress As String = "" Dim strCustCity As String = "" Dim strCustState As String = "" Dim strCustZIPCode As String = "" Dim strCountry As String = "" Dim i As Integer ' Get a reference to the records of the Customers table Dim rows As DataRowCollection = DsBCR1.Tables("Customers").Rows ' Check each record one by one For i = 0 To rows.Count - 1 Step 1 ' Get the current tag number strDrvLicNbr = CStr(rows(i).Item("DrvLicNbr")) ' If the current driver's license number matches the one selected by the user... If strDrvLicNbr = cboCustomerID.Text Then ' Retrieve its corresponding information strCustName = CStr(rows.Item(i).Item("FullName")) strCustAddress = CStr(rows.Item(i).Item("Address")) strCustCity = CStr(rows.Item(i).Item("City")) strCustState = CStr(rows.Item(i).Item("State")) strCustZIPCode = CStr(rows.Item(i).Item("ZIPCode")) strCountry = CStr(rows.Item(i).Item("Country")) ' Since you found a match, STOP!!! End If Next ' Display the details of the customer txtCustName.Text = strCustName txtCustAddress.Text = strCustAddress txtCustCity.Text = strCustCity txtCustState.Text = strCustState txtCustZIPCode.Text = strCustZIPCode txtCustCountry.Text = strCountry End Sub |
Private Sub btnRateApplied_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRateApplied.Click Dim frmRates As RentalRates = New RentalRates frmRates.Show() End Sub |
Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click cboEmployeeID.SelectedIndex = -1 txtRentalOrderID.Text = "" cboCarID.SelectedIndex = -1 txtMake.Text = "" txtModel.Text = "" txtCarYear.Text = "" cboCarCondition.SelectedIndex = 0 cboCustomerID.SelectedIndex = -1 txtCustName.Text = "" txtCustAddress.Text = "" txtCustCity.Text = "" txtCustState.Text = "" txtCustZIPCode.Text = "" txtCustCountry.Text = "USA" cboTankLevel.SelectedIndex = 0 txtRateApplied.Text = "24.95" txtMileage.Text = "0" txtSubTotal.Text = "0.00" dtpStartDate.Value = DateTime.Today txtTaxRate.Text = "7.75" Dim ts As TimeSpan = New TimeSpan(1, 0, 0, 0) dtpEndDate.Value = DateTime.Today.Add(ts) txtTaxAmount.Text = "0.00" txtNumberOfDays.Text = "1" txtOrderTotal.Text = "0.00" cboEmployeeID.Focus() End Sub |
Private Sub dtpEndDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpEndDate.ValueChanged Dim startDate As DateTime = dtpStartDate.Value Dim endDate As DateTime = dtpEndDate.Value Dim diff As TimeSpan = endDate.Subtract(startDate) txtNumberOfDays.Text = CStr(diff.Days + 1) End Sub |
Private Sub btnRateApplied_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRateApplied.Leave Dim rateApplied As Double = CDbl(txtRateApplied.Text) Dim numberOfDays As Integer = CInt(txtNumberOfDays.Text) Dim subTotal As Double = rateApplied * numberOfDays txtSubTotal.Text = subTotal.ToString("F") Dim taxRate As Double = CDbl(txtTaxRate.Text) Dim taxAmount As Double = subTotal * taxRate / 100 txtTaxAmount.Text = taxAmount.ToString("F") Dim orderTotal As Double = subTotal + taxAmount txtOrderTotal.Text = orderTotal.ToString("F") End Sub |
Private Sub btnNewOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click If btnNewOrder.Text = "New Rental Order" Then btnNewOrder.Text = "Save" btnReset_Click(sender, e) Else Dim strInsert As String = "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 & "', '" & _ txtSubTotal.Text & "', '" & _ txtTaxRate.Text & "', '" & _ txtTaxAmount.Text & "', '" & _ txtOrderTotal.Text & "', '" & _ txtNotes.Text & "')" Dim cmdNewOrder As System.Data.SqlClient.SqlCommand = New _ System.Data.SqlClient.SqlCommand(strInsert, SqlConnection1) SqlConnection1.Open() cmdNewOrder.ExecuteNonQuery() SqlConnection1.Close() btnReset_Click(sender, e) btnNewOrder.Text = "New Rental Order" End If End Sub |
|
Private Sub btnRentalOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRentalOrders.Click Dim frmOrders As RentalOrders = New RentalOrders frmOrders.Show() End Sub |
Private Sub btnCars_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCars.Click Dim frmCars As Cars = New Cars frmCars.ShowDialog() End Sub |
Private Sub btnCustomers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCustomers.Click Dim frmCust As Customers = New Customers frmCust.ShowDialog() End Sub |
Private Sub btnEmployees_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmployees.Click Dim frmEmpl As Employees = New Employees frmEmpl.Show() End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click End End Sub |
Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click Dim strReceiptNumber As String = txtRentalOrderID.Text If strReceiptNumber = "" Then MsgBox("You must provide a receipt number to look for a rental order") Exit Sub End If Dim strFindOrder As String = "SELECT * FROM RentalOrders WHERE RentalOrderID = '" & _ CInt(strReceiptNumber) & "'" Dim conDatabase As System.Data.SqlClient.SqlConnection = New _ System.Data.SqlClient.SqlConnection("Data Source=(local);Database='BCR';Integrated Security=yes") Dim cmdDatabase As System.Data.SqlClient.SqlCommand = New _ System.Data.SqlClient.SqlCommand(strFindOrder, conDatabase) conDatabase.Open() Dim rdrRentalOrder As System.Data.SqlClient.SqlDataReader rdrRentalOrder = cmdDatabase.ExecuteReader() While rdrRentalOrder.Read() cboEmployeeID.SelectedIndex = CInt(rdrRentalOrder.GetSqlInt32(1).ToString()) - 1 cboCustomerID.SelectedIndex = CInt(rdrRentalOrder.GetSqlInt32(2).ToString()) - 1 cboCarID.SelectedIndex = CInt(rdrRentalOrder.GetSqlInt32(3).ToString()) - 1 cboCarCondition.Text = rdrRentalOrder.GetString(4) cboTankLevel.Text = rdrRentalOrder.GetString(5) txtMileage.Text = rdrRentalOrder.GetInt32(6).ToString() dtpStartDate.Value = rdrRentalOrder.GetDateTime(7) dtpEndDate.Value = rdrRentalOrder.GetDateTime(8) txtNumberOfDays.Text = rdrRentalOrder.GetInt16(9).ToString() txtRateApplied.Text = rdrRentalOrder.GetDecimal(10).ToString() txtSubTotal.Text = rdrRentalOrder.GetDecimal(11).ToString() txtTaxRate.Text = rdrRentalOrder.GetDecimal(12).ToString() txtTaxAmount.Text = rdrRentalOrder.GetDecimal(13).ToString() txtOrderTotal.Text = rdrRentalOrder.GetDecimal(14).ToString() txtNotes.Text = rdrRentalOrder.GetString(15) End While rdrRentalOrder.Close() conDatabase.Close() End Sub |
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|