|
Views-Based Applications: Bethesda Car Rental |
|
|
A view is a particular way to present the records of one
table, more than one table, another view, or a combination of related tables and
views.
|
To explore views, we will create an application for a
fictitious car rental company.
Practical
Learning: Introducing Views
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click FILE -> New
-> Project...
- In the middle list, click Windows Forms Application and change the
Name to BethesdaCarRental4
- Click OK
- To create a new database, in the Server Explorer, right-click Data
Connections and click Create New SQL Server Database...
- In the Server Name combo box, select your server or type (local)
- Set the database name as BethesdaCarRental1 and
click OK
- Open the
Bethesda Car Rental1 file and select everything in it
- Copy the whole content (to the clipboard)
- In the Server Explorer, right-click the BethesdaCarRental1
connection and click New Query
- Paste it in the Query window
- Right-click inside the document and click Execute
- In the Server Explorer, expand the BethesdaCarRental2 connection
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type BethesdaCarRental.cs and press Enter
twice
- In the Solution Explorer, double-click BethesdaCarRental.cs to
display the form
- Add a button to the form and change its design as follows:
|
Control |
(Name) |
Text |
Button |
|
btnRentalOrders |
Rental Orders ... |
Button |
|
btnVehicles |
Vehicles ... |
Button |
|
btnEmployees |
Employees ... |
Button |
|
btnClose |
Close |
|
- To create a new form, in the Solution Explorer, right-click
BethesdaCarRental1 -> Add -> Windows Form...
- Set the name to RentalRates and press Enter
- Change the properties as follows:
FormBorderStyle:
FixedToolWindow ShowInTaskbar: False
- Add a list view to the form and change its characteristics as
follows:
(Name): lvwRentalRates Dock: Fill FullRowSelect:
True GridLines: True View: Details Columnes:
(Name) |
Text |
TextAlign |
Width |
colCategory |
Category |
|
72 |
colDaily |
Daily |
Right |
|
colWeekly |
Weekly |
Right |
|
colMonthly |
Monthly |
Right |
|
colWeekend |
Weekend |
Right |
742 |
- Click the title bar of the form
- In the Properties window, click Events and double-click Load
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BethesdaCarRental2
{
public partial class RentalRates : Form
{
public RentalRates()
{
InitializeComponent();
}
private void ShowRentalRates()
{
lvwRentalRates.Items.Clear();
using (SqlConnection scBethesdaCarRental = new SqlConnection("Data Source=(local);" +
"Database=BethesdaCarRental1;" +
"Integrated Security=Yes;"))
{
SqlCommand cmdRentalRates = new SqlCommand("SELECT * FROM Assets.Categories;",
scBethesdaCarRental);
scBethesdaCarRental.Open();
SqlDataAdapter sdaRentalRates = new SqlDataAdapter(cmdRentalRates);
DataSet dsRentalRates = new DataSet("RentalRatesSet");
sdaRentalRates.Fill(dsRentalRates);
for (int i = 0; i < dsRentalRates.Tables[0].Rows.Count; i++)
{
ListViewItem lviRentalRate = new ListViewItem(dsRentalRates.Tables[0].Rows[i]["Category"].ToString());
if (i % 2 == 0)
{
lviRentalRate.BackColor = Color.FromArgb(192, 64, 0);
lviRentalRate.ForeColor = Color.White;
}
else
{
lviRentalRate.BackColor = Color.FromArgb(255, 128, 0);
lviRentalRate.ForeColor = Color.Yellow;
}
lviRentalRate.SubItems.Add(dsRentalRates.Tables[0].Rows[i]["Daily"].ToString());
lviRentalRate.SubItems.Add(dsRentalRates.Tables[0].Rows[i]["Weekly"].ToString());
lviRentalRate.SubItems.Add(dsRentalRates.Tables[0].Rows[i]["Monthly"].ToString());
lviRentalRate.SubItems.Add(dsRentalRates.Tables[0].Rows[i]["Weekend"].ToString());
lvwRentalRates.Items.Add(lviRentalRate);
}
}
}
private void RentalRates_Load(object sender, EventArgs e)
{
ShowRentalRates();
}
}
}
- To create a new form, in the Solution Explorer, right-click
BethedaCarRental1 -> Add -> Windows Form...
- Set the Name to NewRentalOrder
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
TextAlign |
Label |
|
|
Rent Start Processed By |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Employee #: |
|
Text Box |
|
txtRentStartProcessedBy |
|
|
Text Box |
|
txtRentStartEmployeeName |
|
|
Label |
|
|
Processed For - Customer Information |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Order Timing |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
First Name: |
|
Text Box |
|
txtCustomerFirstName |
|
|
Label |
|
|
Last Name: |
|
Text Box |
|
txtCustomerLastName |
|
|
Label |
|
|
Start Date: |
|
Date Time Picker |
|
dtpStartDate |
|
|
Label |
|
|
Address: |
|
Text Box |
|
txtCustomerAddress |
|
|
Label |
|
|
City: |
|
Text Box |
|
txtCustomerCity |
|
|
Label |
|
|
Order Evaluation |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
State/Province: |
|
Combo Box |
|
cbxCustomerStates |
|
Items: AB, AL, AK, AZ, AR, BC, CA, CO, CT,
DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA,
ME, MD, MA, MI, MN, MS, MO, MT, NB, NE, NV, NH,
NJ, NL, NM, NS, NY, NC, ND, OH, OK, ON, OR, PA,
PE, QC, RI, SC, SD, SK, TN, TX, UT, VT, VA, WA,
WV, WI, WY |
Label |
|
|
ZIP/Postal Code: |
|
Text Box |
|
txtCustomerZIPCode |
|
|
Label |
|
|
Rate Applied: |
|
Text Box |
|
txtRateApplied |
0.00 |
TextAlign: Right |
Button |
|
btnRentalRates |
Rental Rates... |
|
Label |
|
|
Vehicle Selected |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Tag Number: |
|
Text Box |
|
txtTagNumber |
|
|
Label |
|
|
Tank Level: |
|
Combo Box |
|
cbxTankLevels |
|
Items: Empty 1/4 Empty Half Tank 3/4
Full Full |
Label |
|
|
Vehicle: |
|
Text Box |
|
txtVehicleSelected |
|
|
Label |
|
|
Mileage Start: |
|
Text Box |
|
txtMileageStart |
|
|
Label |
|
|
Condition: |
|
Combo Box |
|
cbxConditions |
|
Items: Drivable Excellent Needs Repair |
Label |
|
|
Order Status: |
|
Combo Box |
|
cbxOrdersStatus |
|
Items: Unknown Processing Order Complete
Vehicle With Customer |
Label |
|
|
Notes |
|
Text Box |
|
txtNotes |
|
Multiline: True ScrollBars: Verticle |
Button |
|
btnSave |
Save |
|
Button |
|
btnPrint |
Print... |
|
Button |
|
btnPrintPreview |
Print Preview |
|
Button |
|
btnClose |
Close |
|
|
- From the Printing section of the Toolbox, click PrintDocument and
click the form
- In the Properties window, set its (Name) to docPrint and
press Enter
- Under the form, double-click docPrint and implement its event as
follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BethesdaCarRental2
{
public partial class NewRentalOrder : Form
{
public NewRentalOrder()
{
InitializeComponent();
}
private void docPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 90, 750, 90);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 93, 750, 93);
string strDisplay = "Bethesda Car Rental";
System.Drawing.Font fntString = new Font("Times New Roman", 28, FontStyle.Bold);
e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 240, 100);
strDisplay = "Car Rental Order";
fntString = new System.Drawing.Font("Times New Roman", 22, FontStyle.Regular);
e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 320, 150);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 187, 750, 187);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 190, 750, 190);
fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Processed By: ", fntString, Brushes.Black, 100, 220);
fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtRentStartProcessedBy.Text + ": " + txtRentStartEmployeeName.Text, fntString, Brushes.Black, 260, 220);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 240, 720, 240);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 260, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 260, 620, 20));
e.Graphics.DrawString("Customer", fntString, Brushes.White, 100, 260);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("First Name: ", fntString, Brushes.Black, 100, 300);
e.Graphics.DrawString("Last Name: ", fntString, Brushes.Black, 420, 300);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtCustomerFirstName.Text, fntString, Brushes.Black, 260, 300);
e.Graphics.DrawString(txtCustomerLastName.Text, fntString, Brushes.Black, 540, 300);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 320, 720, 320);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Address: ", fntString, Brushes.Black, 100, 330);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtCustomerAddress.Text, fntString, Brushes.Black, 260, 330);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 350, 720, 350);
strDisplay = txtCustomerCity.Text + " " + cbxCustomerStates.Text + " " + txtCustomerZIPCode.Text;
fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 260, 360);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 260, 380, 720, 380);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 410, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 410, 620, 20));
e.Graphics.DrawString("Vehicle Information", fntString, Brushes.White, 100, 410);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Tag #: ", fntString, Brushes.Black, 100, 450);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtTagNumber.Text, fntString, Brushes.Black, 260, 450);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 470, 720, 470);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Vehicle: ", fntString, Brushes.Black, 100, 480);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtVehicleSelected.Text, fntString, Brushes.Black, 260, 480);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 500, 720, 500);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Vehicle Condition: ", fntString, Brushes.Black, 100, 510);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(cbxConditions.Text, fntString, Brushes.Black, 260, 510);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 530, 380, 530);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Tank Level: ", fntString, Brushes.Black, 420, 510);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(cbxTankLevels.Text, fntString, Brushes.Black, 530, 510);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 530, 720, 530);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Mileage Start:", fntString, Brushes.Black, 100, 540);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtMileageStart.Text, fntString, Brushes.Black, 260, 540);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 560, 720, 560);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 590, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 590, 620, 20));
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Order Timing Information", fntString, Brushes.White, 100, 590);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Start Date:", fntString, Brushes.Black, 100, 620);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(dtpStartDate.Value.ToString("D"), fntString, Brushes.Black, 260, 620);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 640, 720, 640);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 700, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 700, 620, 20));
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Order Evaluation", fntString, Brushes.White, 100, 700);
StringFormat fmtString = new StringFormat();
fmtString.Alignment = StringAlignment.Far;
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Rate Applied:", fntString, Brushes.Black, 100, 740);
fntString = new Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString(txtRateApplied.Text, fntString, Brushes.Black, 300, 740, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 380, 760);
}
}
}
- Return to the New Rental Order form
- From the Printing section of the Toolbox, click PrintDialog and
click the form
- In the Properties window, change its Name to dlgPrint
- Still in the Properties windows, set its Document property to
docPrint
- On the Order Processing form, double-click the Print button and
implement its event as follows:
private void btnPrint_Click(object sender, EventArgs e)
{
if (dlgPrint.ShowDialog() == DialogResult.OK)
docPrint.Print();
}
- Return to the New Rental Order form
- From the Printing section of the Toolbox, click PrintPreviewDialog
and click the form
- In the Properties window, change its (Name) to dlgPrintPreview
- Still in the Properties windows, set its Document property to
docPrint
- On the New Rental Order form, double-click the Print Preview
button
- Implement the event as follows:
private void btnPrintPreview_Click(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
- Return to the New Rental Order form and double-click the Rental
Rates button
- Implement the event as follows:
private void btnRentalRates_Click(object sender, EventArgs e)
{
RentalRates rrs = new RentalRates();
rrs.Show();
}
- Return to the New Rental Order form
- Under the form, select docPrint, dlgPrint, and dlgPrintPreview
(you can click one, press and hold either Ctrl or Shift, then click
each of the others, and release Ctrl or Shift)
- Press Ctrl + C to copy
- To create a new form, in the Solution Explorer, right-click
BethedaCarRental1 -> Add -> Windows Form...
- Set the Name to UpdateRentalOrder
- Click Add
- Click the middle of the form and press Ctrl + V to paste
- Design the form as follows:
|
Control |
(Name) |
Text |
TextAlign |
Label |
|
|
Rent Start Processed By |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Rent End Processed By |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Employee #: |
|
Text Box |
|
txtRentStartProcessedBy |
|
|
Text Box |
|
txtRentStartEmployeeName |
|
|
Label |
|
|
Employee #: |
|
Text Box |
|
txtRentEndProcessedBy |
|
|
Text Box |
|
txtRentEndEmployeeName |
|
|
Label |
|
|
Processed For Customer |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Order Timing |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
First Name: |
|
Text Box |
|
txtCustomerFirstName |
|
|
Label |
|
|
Last Name: |
|
Text Box |
|
txtCustomerLastName |
|
|
Label |
|
|
Start Date: |
|
Date Time Picker |
|
dtpStartDate |
|
|
Label |
|
|
Address: |
|
Text Box |
|
txtCustomerAddress |
|
|
Label |
|
|
End Date: |
|
Date Time Picker |
|
dtpEndDate |
|
|
Label |
|
|
City: |
|
Text Box |
|
txtCustomerCity |
|
|
Label |
|
|
Total Days: |
|
Text Box |
|
txtTotalDays |
0.00 |
TextAlign: Right |
Label |
|
|
State/Province: |
|
Combo Box |
|
cbxCustomerStates |
|
Items: AB, AL, AK, AZ, AR, BC, CA, CO, CT,
DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA,
ME, MD, MA, MI, MN, MS, MO, MT, NB, NE, NV, NH,
NJ, NL, NM, NS, NY, NC, ND, OH, OK, ON, OR, PA,
PE, QC, RI, SC, SD, SK, TN, TX, UT, VT, VA, WA,
WV, WI, WY |
Label |
|
|
ZIP/Postal Code: |
|
Text Box |
|
txtCustomerZIPCode |
|
|
Label |
|
|
Vehicle Selected |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Order Evaluation |
AutoSize: False BackColor: Gray BorderStyle:
FixedSingle ForeColor: White |
Label |
|
|
Tag Number: |
|
Text Box |
|
txtTagNumber |
|
|
Label |
|
|
Tank Level: |
|
Combo Box |
|
cbxTankLevels |
|
Items: Empty 1/4 Empty Half Tank 3/4
Full Full |
Label |
|
|
Rate Applied: |
|
Text Box |
|
txtRateApplied |
0.00 |
TextAlign: Right |
Button |
|
btnRentalRates |
Rental Rates... |
|
Label |
|
|
Vehicle: |
|
Text Box |
|
txtVehicleSelected |
|
|
Label |
|
|
Sub-Total: |
|
Text Box |
|
txtSubTotal |
0.00 |
TextAlign: Right |
Button |
|
btnCalculate |
Calculate |
|
Label |
|
|
Mileage Start: |
|
Text Box |
|
txtMileageStart |
|
|
Label |
|
|
Mileage End: |
|
Text Box |
|
txtMileageEnd |
|
|
Label |
|
|
Tax Rate: |
|
Text Box |
|
txtTaxRate |
7.70 |
TextAlign: Right |
Label |
|
|
% |
|
Label |
|
|
Condition: |
|
Combo Box |
|
cbxConditions |
|
Items: Drivable Excellent Needs Repair |
Label |
|
|
Mileage Total: |
|
Text Box |
|
txtMileageTotal |
|
TextAlign: Right |
Label |
|
|
Tax Amount: |
|
Text Box |
|
txtTaxAmount |
0.00 |
TextAlign: Right |
Button |
|
btnPrint |
Print... |
|
Label |
|
|
Order Status: |
|
Combo Box |
|
cbxOrdersStatus |
|
Items: Unknown Processing Order Complete
Vehicle With Customer |
Label |
|
|
Order Total: |
|
Text Box |
|
txtOrderTotal |
0.00 |
TextAlign: Right |
Button |
|
btnPrintPreview |
Print Preview |
|
Label |
|
|
Notes |
|
Text Box |
|
txtNotes |
|
Multiline: True ScrollBars: Verticle |
Label |
|
|
Receipt # |
|
Text Box |
|
txtReceiptNumber |
0 |
|
Button |
|
btnOpen |
Open |
|
Button |
|
btnUpdateRentalOrder |
Update Rental Order |
|
Button |
|
btnResetForm |
Reset Form |
|
Button |
|
btnClose |
Close |
|
|
- Under the form, double-click docPrint and implement its event as
follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BethesdaCarRental2
{
public partial class UpdateRentalOrder : Form
{
public UpdateRentalOrder()
{
InitializeComponent();
}
private void docPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 90, 750, 90);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 93, 750, 93);
string strDisplay = "Bethesda Car Rental";
System.Drawing.Font fntString = new Font("Times New Roman", 28, FontStyle.Bold);
e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 240, 100);
strDisplay = "Car Rental Order";
fntString = new System.Drawing.Font("Times New Roman", 22, FontStyle.Regular);
e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 320, 150);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 187, 750, 187);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 190, 750, 190);
System.Drawing.Font boldString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Bold);
System.Drawing.Font regularString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
e.Graphics.DrawString("Receipt #: ", boldString, Brushes.Black, 100, 220);
e.Graphics.DrawString(txtReceiptNumber.Text, regularString, Brushes.Black, 260, 220);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 240, 380, 240);
e.Graphics.DrawString("Processed By: ", boldString, Brushes.Black, 420, 220);
e.Graphics.DrawString(txtRentStartEmployeeName.Text, regularString, Brushes.Black, 550, 220);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 240, 720, 240);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 260, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 260, 620, 20));
e.Graphics.DrawString("Customer", boldString, Brushes.White, 100, 260);
e.Graphics.DrawString("First Name: ", boldString, Brushes.Black, 100, 300);
e.Graphics.DrawString("Last Name: ", boldString, Brushes.Black, 420, 300);
e.Graphics.DrawString(txtCustomerFirstName.Text, regularString, Brushes.Black, 260, 300);
e.Graphics.DrawString(txtCustomerLastName.Text, regularString, Brushes.Black, 540, 300);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 320, 720, 320);
e.Graphics.DrawString("Address: ", boldString, Brushes.Black, 100, 330);
e.Graphics.DrawString(txtCustomerAddress.Text, regularString, Brushes.Black, 260, 330);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 350, 720, 350);
strDisplay = txtCustomerCity.Text + " " + cbxCustomerStates.Text + " " + txtCustomerZIPCode.Text;
e.Graphics.DrawString(strDisplay, regularString, Brushes.Black, 260, 360);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 260, 380, 720, 380);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 410, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 410, 620, 20));
e.Graphics.DrawString("Vehicle Information", boldString, Brushes.White, 100, 410);
e.Graphics.DrawString("Tag #: ", boldString, Brushes.Black, 100, 450);
e.Graphics.DrawString(txtTagNumber.Text, regularString, Brushes.Black, 260, 450);
e.Graphics.DrawString("Make & Model: ", boldString, Brushes.Black, 420, 450);
e.Graphics.DrawString(txtVehicleSelected.Text, regularString, Brushes.Black, 540, 450);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 470, 720, 470);
StringFormat fmtString = new StringFormat();
fmtString.Alignment = StringAlignment.Far;
e.Graphics.DrawString("Vehicle Condition: ", boldString, Brushes.Black, 100, 480);
e.Graphics.DrawString(cbxConditions.Text, regularString, Brushes.Black, 260, 480);
e.Graphics.DrawString("Mileage Start:", boldString, Brushes.Black, 420, 480);
e.Graphics.DrawString(txtMileageStart.Text, regularString, Brushes.Black, 600, 480, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 500, 720, 500);
e.Graphics.DrawString("Tank Level: ", boldString, Brushes.Black, 100, 510);
e.Graphics.DrawString(cbxTankLevels.Text, regularString, Brushes.Black, 260, 510);
e.Graphics.DrawString("Mileage End:", boldString, Brushes.Black, 420, 510);
e.Graphics.DrawString(txtMileageEnd.Text, regularString, Brushes.Black, 600, 510, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 530, 720, 530);
e.Graphics.DrawString("Mileage Total: ", boldString, Brushes.Black, 420, 540);
e.Graphics.DrawString(txtMileageTotal.Text, regularString, Brushes.Black, 600, 540, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 560, 720, 560);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 590, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 590, 620, 20));
e.Graphics.DrawString("Order Timing Information", boldString, Brushes.White, 100, 590);
e.Graphics.DrawString("Start Date:", boldString, Brushes.Black, 100, 620);
e.Graphics.DrawString(dtpStartDate.Value.ToString("D"), regularString, Brushes.Black, 260, 620);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 640, 720, 640);
e.Graphics.DrawString("End Date:", boldString, Brushes.Black, 100, 650);
e.Graphics.DrawString(dtpEndDate.Value.ToString("D"), regularString, Brushes.Black, 260, 650);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 670, 520, 670);
e.Graphics.DrawString("Total Days:", boldString, Brushes.Black, 550, 650);
e.Graphics.DrawString(txtTotalDays.Text, regularString, Brushes.Black, 700, 650, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 550, 670, 720, 670);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 700, 620, 20));
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 700, 620, 20));
e.Graphics.DrawString("Order Evaluation", boldString, Brushes.White, 100, 700);
e.Graphics.DrawString("Rate Applied:", boldString, Brushes.Black, 100, 740);
e.Graphics.DrawString(txtRateApplied.Text, regularString, Brushes.Black, 300, 740, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 380, 760);
e.Graphics.DrawString("Tax Rate:", boldString, Brushes.Black, 420, 740);
e.Graphics.DrawString(txtTaxRate.Text, regularString, Brushes.Black, 640, 740, fmtString);
e.Graphics.DrawString("%", regularString, Brushes.Black, 640, 740);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 760, 720, 760);
e.Graphics.DrawString("Sub-Total:", boldString, Brushes.Black, 100, 770);
e.Graphics.DrawString(txtSubTotal.Text, regularString, Brushes.Black, 300, 770, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 790, 380, 790);
e.Graphics.DrawString("Tax Amount:", boldString, Brushes.Black, 420, 770);
e.Graphics.DrawString(txtTaxAmount.Text, regularString, Brushes.Black, 640, 770, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 790, 720, 790);
e.Graphics.DrawString("Order Total:", boldString, Brushes.Black, 420, 800);
e.Graphics.DrawString(txtOrderTotal.Text, regularString, Brushes.Black, 640, 800, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 820, 720, 820);
e.Graphics.DrawString("Order Status:", boldString, Brushes.Black, 100, 830);
e.Graphics.DrawString(cbxOrdersStatus.Text, regularString, Brushes.Black, 260, 830);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 850, 720, 850);
e.Graphics.DrawString("Notes:", boldString, Brushes.Black, 100, 860);
e.Graphics.DrawString(txtNotes.Text, regularString, Brushes.Black, 260, 860);
}
}
}
- Return the Update Rental Order form and double-click the Print
button
- Implement its event as follows:
private void btnPrint_Click(object sender, EventArgs e)
{
if (dlgPrint.ShowDialog() == DialogResult.OK)
docPrint.Print();
}
- Return to the Update Rental Order form and double-click the Print
Preview button
- Implement the event as follows:
private void btnPrintPreview_Click(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
- Return to the Update Rental Order form and double-click the Rental
Rates button
- Implement the event as follows:
private void btnRentalRates_Click(object sender, EventArgs e)
{
RentalRates rrs = new RentalRates();
rrs.Show();
}
- Return to the Update Rental Order form and click the Mileage End
text box
- In the Events section of the Properties window, double-click Leave
and implement the event as follows:
private void txtMileageEnd_Leave(object sender, EventArgs e)
{
int mileageStart = 0, mileageEnd = 0, mileageTotal;
if (string.IsNullOrEmpty(txtMileageStart.Text))
return;
if (string.IsNullOrEmpty(txtMileageEnd.Text))
return;
try
{
mileageStart = int.Parse(txtMileageStart.Text);
}
catch (FormatException fe)
{
MessageBox.Show("The value you provided for the mileage start is not valid.\n" +
"Please report the error as: " + fe.Message,
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
mileageEnd = int.Parse(txtMileageEnd.Text);
mileageTotal = mileageEnd - mileageStart;
txtMileageTotal.Text = mileageTotal.ToString();
}
catch (FormatException fe)
{
MessageBox.Show("The value you provided for the mileage end is not valid.\n" +
"Please report the error as: " + fe.Message,
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
- Return to the form and double-click the Start Date date time
picker
- Implement the event as follows:
private void dtpStartDate_ValueChanged(object sender, EventArgs e)
{
dtpEndDate.Value = dtpStartDate.Value;
}
- Return to the form and double-click the End Date date time picker
- Implement the event as follows:
private void dtpEndDate_ValueChanged(object sender, EventArgs e)
{
int days;
DateTime dteStart = this.dtpStartDate.Value;
DateTime dteEnd = this.dtpEndDate.Value;
// Let's calculat the difference in days
TimeSpan tme = dteEnd - dteStart;
days = tme.Days;
// If the customer returns the car the same day,
// we consider that the car was rented for 1 day
if (days == 0)
days = 1;
txtTotalDays.Text = days.ToString();
// At any case, we will let the clerk specify the actual number of days
}
- Return to the form and double-click the Rental Rates button
- Implement the event as follows:
private void btnRentalRates_Click(object sender, EventArgs e)
{
RentalRates rrs = new RentalRates();
rrs.Show();
}
- Return to the form and click the Rate Applied text box
- In the Events section of the Properties window, double-click Leave
and implement the event as follows:
private void txtRateApplied_Leave(object sender, EventArgs e)
{
int days = 0;
double rateApplied = 0D;
if (string.IsNullOrEmpty(txtRateApplied.Text))
return;
try
{
rateApplied = double.Parse(txtRateApplied.Text);
days = int.Parse(txtTotalDays.Text);
txtSubTotal.Text = (rateApplied * days).ToString("F");
}
catch (FormatException fe)
{
MessageBox.Show("The value you provided for the rate is not valid.\n" +
"Please report the error as: " + fe.Message,
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
- Return to the form and double-click the Calculate button
- Implement the event as follows:
private void btnCalculate_Click(object sender, EventArgs e)
{
int Days = 0;
double RateApplied = 0.00;
double SubTotal = 0.00;
double TaxRate = 0.00;
double TaxAmount = 0.00;
double OrderTotal = 0.00;
try
{
Days = int.Parse(txtTotalDays.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Number of Days",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
RateApplied = double.Parse(txtRateApplied.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Amount for Rate Applied",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
SubTotal = Days * RateApplied;
txtSubTotal.Text = SubTotal.ToString("F");
try
{
TaxRate = double.Parse(txtTaxRate.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Tax Rate",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
TaxAmount = SubTotal * TaxRate / 100;
txtTaxAmount.Text = TaxAmount.ToString("F");
OrderTotal = SubTotal + TaxAmount;
txtOrderTotal.Text = OrderTotal.ToString("F");
}
- Return to the form and double-click the Reset Form button
- Implement the event as follows:
private void btnResetForm_Click(object sender, EventArgs e)
{
txtReceiptNumber.Text = "";
txtRentStartProcessedBy.Text = "";
txtRentStartEmployeeName.Text = "";
txtRentEndProcessedBy.Text = "";
txtRentEndEmployeeName.Text = "";
txtCustomerFirstName.Text = "";
txtCustomerLastName.Text = "";
txtCustomerAddress.Text = "";
txtCustomerCity.Text = "";
cbxCustomerStates.Text = "";
txtCustomerZIPCode.Text = "";
txtTagNumber.Text = "";
txtCustomerAddress.Text = "";
txtCustomerCity.Text = "";
cbxCustomerStates.Text = "";
txtCustomerZIPCode.Text = "";
cbxConditions.SelectedIndex = 0;
cbxTankLevels.SelectedIndex = 0;
txtMileageStart.Text = "";
txtMileageEnd.Text = "";
txtMileageTotal.Text = "";
txtRateApplied.Text = "";
txtTaxRate.Text = "7.70";
txtTaxAmount.Text = "0.00";
txtSubTotal.Text = "0.00";
txtOrderTotal.Text = "";
dtpStartDate.Value = DateTime.Today;
dtpEndDate.Value = DateTime.Today;
txtTotalDays.Text = "0.00";
cbxOrdersStatus.SelectedIndex = 0;
txtNotes.Text = "";
}
The simplest formula to create a view
is as follows:
CREATE VIEW [Schema].ViewName
AS
SELECT Statement
Practical
Learning: Creating a View
|
|
- Click inside the SQL Query window and press Ctrl + A
- To create a view, type the following code:
USE BethesdaCarRental1;
GO
CREATE VIEW Assets.Cars
AS
SELECT TagNumber,
Category,
Make,
Model,
Doors,
Passengers,
Condition,
Availability
FROM Assets.Vehicles
GO
- Right-click inside the SQL Query window and click Execute
- To create a new form, in the Solution Explorer, right-click
BethedaCarRental2 -> Add -> Windows Form...
- Set the Name to Vehicles
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
List View |
|
lvwVehicles |
|
FullRowSelect: True GridLines: True |
|
|
(Name) |
Text |
TextAlign |
Width |
colTagNumber |
Tag # |
|
|
colCategory |
Category |
|
70 |
colMake |
Make |
|
70 |
colModel |
Model |
|
100 |
colDoors |
Doors |
Right |
40 |
colPassengers |
Passengers |
Right |
68 |
colCondition |
Condition |
|
|
colAvailability |
Availability |
|
85 |
|
Button |
|
btnClose |
Close |
|
|
- Double-click an unoccupied area of the form
- Return to the form and double-click the Close button
- change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BethesdaCarRental2
{
public partial class Vehicles : Form
{
public Vehicles()
{
InitializeComponent();
}
private void ShowVehicles()
{
lvwVehicles.Items.Clear();
using (SqlConnection scBethesdaCarRental = new SqlConnection("Data Source=(local);" +
"Database=BethesdaCarRental12;" +
"Integrated Security=Yes;"))
{
SqlCommand cmdVehicles = new SqlCommand("SELECT * FROM Assets.Cars;",
scBethesdaCarRental);
scBethesdaCarRental.Open();
SqlDataAdapter sdaVehicles = new SqlDataAdapter(cmdVehicles);
DataSet dsVehicles = new DataSet("SubCategoriesSet");
sdaVehicles.Fill(dsVehicles);
foreach (DataRow drVehicle in dsVehicles.Tables[0].Rows)
{
ListViewItem lviVehicle = new ListViewItem(drVehicle["TagNumber"].ToString());
lviVehicle.SubItems.Add(drVehicle["Category"].ToString());
lviVehicle.SubItems.Add(drVehicle["Make"].ToString());
lviVehicle.SubItems.Add(drVehicle["Model"].ToString());
lviVehicle.SubItems.Add(drVehicle["Doors"].ToString());
lviVehicle.SubItems.Add(drVehicle["Passengers"].ToString());
lviVehicle.SubItems.Add(drVehicle["Condition"].ToString());
lviVehicle.SubItems.Add(drVehicle["Availability"].ToString());
lvwVehicles.Items.Add(lviVehicle);
}
}
}
private void Cars_Load(object sender, EventArgs e)
{
ShowVehicles();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
The formula to programmatically delete a view in SQL
is:
DROP VIEW [Schema.]ViewName
Besides the ability to select records, views can be
used to add records to a table:
Practical
Learning: Performing Data Entry Using a View
|
|
- Click inside the SQL Query window and press Ctrl + A
- To create a view, type the following code:
USE BethesdaCarRental1;
GO
CREATE VIEW Transactions.NewRentalOrders
AS
SELECT RentStartProcessedBy,
RentEndProcessedBy,
CustomerFirstName,
CustomerLastName,
CustomerAddress,
CustomerCity,
CustomerState,
CustomerZIPCode,
TagNumber,
Condition,
TankLevel,
MileageStart,
StartDate,
RateApplied,
OrderStatus,
Notes
FROM Transactions.RentalOrders;
GO
- Right-click inside the SQL Query window and click Execute
- Display the New Rental Order form and double-click the Save button
- Change the document as follows:
private void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtRentStartProcessedBy.Text))
{
MessageBox.Show("You must specify the employee who processed the rental order.",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(txtTagNumber.Text))
{
MessageBox.Show("You must specify the car that was rented.",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (SqlConnection scBethesdaCarRental =
new SqlConnection(@"Data Source='CENTRAL\MSSQLSERVER2012';" +
"Database='BethesdaCarRental1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdRentalOrders = new SqlCommand("INSERT INTO Transactions.NewRentalOrders(" +
"RentStartProcessedBy, RentEndProcessedBy, CustomerFirstName, CustomerLastName, " +
"CustomerAddress, CustomerCity, CustomerState, CustomerZIPCode, TagNumber, " +
"Condition, TankLevel, MileageStart, StartDate, RateApplied, OrderStatus) VALUES(N'" +
txtRentStartProcessedBy.Text + "', N'" + txtRentStartProcessedBy.Text + "', N'" +
txtCustomerFirstName.Text + "', N'" + txtCustomerLastName.Text + "', N'" +
txtCustomerAddress.Text + "', N'" + txtCustomerCity.Text + "', N'" +
cbxCustomerStates.Text + "', N'" + txtCustomerZIPCode.Text + "', N'" +
txtTagNumber.Text + "', N'" + cbxConditions.Text + "', N'" + cbxTankLevels.Text +
"', N'" + txtMileageStart.Text + "', N'" + dtpStartDate.Value.ToShortDateString() + "', " +
double.Parse(txtRateApplied.Text) + ", N'" + cbxOrdersStatus.Text + "');",
scBethesdaCarRental);
scBethesdaCarRental.Open();
cmdRentalOrders.ExecuteNonQuery();
MessageBox.Show("A new rental order has been created.",
"Bethesda Car Rental",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Updating Records Using a View
|
|
Besides being used as intermediary for data entry, a
view can be specially created to update one or more records of a table.
|
|