XML-Based Applications: College Park Auto-Shop: Cpas2
XML-Based Applications: College Park Auto-Shop: Cpas2
Auto-Shop
Introduction
As always mentioned, XML provides the ability to store information as regular text so
that the same information can be accessed from completely independent applications.
This provides tremendous opportunities in the areas of storage and sharing of data. To
make it even more useful, the .NET Framework provides full support for the Document
Object Model (DOM) of XML through various classes.
In this exercise, we are going to create an application used by a car repair business.
To use it, an employee of the business can open a form to process a customer's work
order. During this, the employee can enter the customer, the information about the car
to be fixed, and a text description of the problem.
6. Click OK
7. While the new dataSet1 button is selected, in the Properties window, set its
(Name) to dsWorkorders and set its DataSetName to Workorders
8. To create the necessary structure of a work order, while the DataSet object is still
selected, in the Properties window, click Tables and click its ellipsis button
9. In the Tables Collection Editor, click Add
10. Set the (Name) to tblWorkorder and set the TableName to Workorder
11. Set its Modifiers to Public
12. To create the columns, in the Workorder Properties section, click Columns and
click its ellipsis button
13. In the Columns Collection Editor, click Add
14. Set the properties as follows:
(Name): colCustomerName
ColumnName: CustomerName
Modifiers: Public
15. In the same way, create the other columns as follows:
Besides the parts used, the employee must also list the types of repairs that were performed
and their costs. This also allows the customer to know what was done.
Once the parts information and the jobs performed have been entered, the order can be
calculated to know the total the customer will pay.
Practical Learning: Starting the Exercise
1. Display the New Work Order form. Double-click the Reset Order button and
implement its Click event as follows:
this.txtPartName1.Text = "";
this.txtUnitPrice1.Text = "0.00";
this.txtQuantity1.Text = "0";
this.txtSubTotal1.Text = "0.00";
this.txtPartName2.Text = "";
this.txtUnitPrice2.Text = "0.00";
this.txtQuantity2.Text = "0";
this.txtSubTotal2.Text = "0.00";
this.txtPartName3.Text = "";
this.txtUnitPrice3.Text = "0.00";
this.txtQuantity3.Text = "0";
this.txtSubTotal3.Text = "0.00";
this.txtPartName4.Text = "";
this.txtUnitPrice4.Text = "0.00";
this.txtQuantity4.Text = "0";
this.txtSubTotal4.Text = "0.00";
this.txtPartName5.Text = "";
this.txtUnitPrice5.Text = "0.00";
this.txtQuantity5.Text = "0";
this.txtSubTotal5.Text = "0.00";
this.txtJobPerformed1.Text = "";
this.txtJobPrice1.Text = "0.00";
this.txtJobPerformed2.Text = "";
this.txtJobPrice2.Text = "0.00";
this.txtJobPerformed3.Text = "";
this.txtJobPrice3.Text = "0.00";
this.txtJobPerformed4.Text = "";
this.txtJobPrice4.Text = "0.00";
this.txtJobPerformed5.Text = "";
this.txtJobPrice5.Text = "0.00";
this.txtTotalParts.Text = "0.00";
this.txtTotalLabor.Text = "0.00";
this.txtTaxRate.Text = "7.75";
this.txtTaxAmount.Text = "0.00";
this.txtTotalOrder.Text = "0.00";
this.txtRecommendations.Text = "";
this.txtCustomerName.Focus();
}
2. Double-click the Calculate Order button and implement its Click event as follows:
try
{
part1Quantity =
int.Parse(this.txtQuantity1.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity1.Text = "0";
this.txtQuantity1.Focus();
}
}
try
{
part2Quantity =
int.Parse(this.txtQuantity2.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity2.Text = "0";
this.txtQuantity2.Focus();
}
}
try
{
part3Quantity =
int.Parse(this.txtQuantity3.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity3.Text = "0";
this.txtQuantity3.Focus();
}
}
try
{
part4Quantity =
int.Parse(this.txtQuantity4.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity4.Text = "0";
this.txtQuantity4.Focus();
}
}
try
{
part5Quantity =
int.Parse(this.txtQuantity5.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity5.Text = "0";
this.txtQuantity5.Focus();
}
}
// Don't bill the customer for a job that is not specified
if( this.txtJobPerformed1.Text == "" )
{
this.txtJobPrice1.Text = "0.00";
job1Price = 0.00M;
}
else
{
try
{
job1Price =
decimal.Parse(this.txtJobPrice1.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice1.Text = "0.00";
this.txtJobPrice1.Focus();
}
}
this.txtSubTotal1.Text = part1SubTotal.ToString("F");
this.txtSubTotal2.Text = part2SubTotal.ToString("F");
this.txtSubTotal3.Text = part3SubTotal.ToString("F");
this.txtSubTotal4.Text = part4SubTotal.ToString("F");
this.txtSubTotal5.Text = part5SubTotal.ToString("F");
try
{
taxRate = decimal.Parse(this.txtTaxRate.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Tax Rate");
this.txtTaxRate.Text = "7.75";
this.txtTaxRate.Focus();
}
this.txtTotalParts.Text = totalParts.ToString("F");
this.txtTotalLabor.Text = totalLabor.ToString("F");
this.txtTaxAmount.Text = taxAmount.ToString("F");
this.txtTotalOrder.Text = totalOrder.ToString("F");
}
3. Execute the application to test it and fill the form with a record
4. Click the Calculate Order button
5. Close the form and return to your programming environment
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
4. Implement the event as follows:
private void btnSaveOrder_Click(object sender, System.EventArgs e)
{
// Get the date that the Order Date displays
DateTime tmeToday = this.dtpOrderDate.Value;
int day = tmeToday.Day;
int month = tmeToday.Month;
int year = tmeToday.Year;
string[] strMonth = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
string strFilename = day.ToString() +
strMonth[month-1] +
year.ToString() + ".xml";
rowNewOrder["CustomerName"] =
this.txtCustomerName.Text;
rowNewOrder["Addres"] = this.txtAddress.Text;
rowNewOrder["City"] = this.txtCity.Text;
rowNewOrder["State"] = this.txtState.Text;
rowNewOrder["ZIPCode"] = this.txtZIPCode.Text;
rowNewOrder["Make"] = this.txtMake.Text;
rowNewOrder["Model"] = this.txtModel.Text;
rowNewOrder["CarYear"] = this.txtCarYear.Text;
rowNewOrder["Problem"] = this.txtProblem.Text;
rowNewOrder["PartName1"] = this.txtPartName1.Text;
rowNewOrder["UnitPrice1"] = this.txtUnitPrice1.Text;
rowNewOrder["Quantity1"] = this.txtQuantity1.Text;
rowNewOrder["SubTotal1"] = this.txtSubTotal1.Text;
rowNewOrder["PartName2"] = this.txtPartName2.Text;
rowNewOrder["UnitPrice2"] = this.txtUnitPrice2.Text;
rowNewOrder["Quantity2"] = this.txtQuantity2.Text;
rowNewOrder["SubTotal2"] = this.txtSubTotal2.Text;
rowNewOrder["PartName3"] = this.txtPartName3.Text;
rowNewOrder["UnitPrice3"] = this.txtUnitPrice3.Text;
rowNewOrder["Quantity3"] = this.txtQuantity3.Text;
rowNewOrder["SubTotal3"] = this.txtSubTotal3.Text;
rowNewOrder["PartName4"] = this.txtPartName4.Text;
rowNewOrder["UnitPrice4"] = this.txtUnitPrice4.Text;
rowNewOrder["Quantity4"] = this.txtQuantity4.Text;
rowNewOrder["SubTotal4"] = this.txtSubTotal4.Text;
rowNewOrder["PartName5"] = this.txtPartName5.Text;
rowNewOrder["UnitPrice5"] = this.txtUnitPrice5.Text;
rowNewOrder["Quantity5"] = this.txtQuantity5.Text;
rowNewOrder["SubTotal5"] = this.txtSubTotal5.Text;
rowNewOrder["JobPerformed1"] =
this.txtJobPerformed1.Text;
rowNewOrder["JobPrice1"] = this.txtJobPrice1.Text;
rowNewOrder["JobPerformed2"] =
this.txtJobPerformed2.Text;
rowNewOrder["JobPrice2"] = this.txtJobPrice2.Text;
rowNewOrder["JobPerformed3"] =
this.txtJobPerformed3.Text;
rowNewOrder["JobPrice3"] = this.txtJobPrice3.Text;
rowNewOrder["JobPerformed4"] =
this.txtJobPerformed4.Text;
rowNewOrder["JobPrice4"] = this.txtJobPrice4.Text;
rowNewOrder["JobPerformed5"] =
this.txtJobPerformed5.Text;
rowNewOrder["JobPrice5"] = this.txtJobPrice5.Text;
rowNewOrder["Recommendation"] =
this.txtRecommendations.Text;
rowNewOrder["TotalPart"] = this.txtTotalParts.Text;
rowNewOrder["TotalLabor"] = this.txtTotalLabor.Text;
rowNewOrder["TaxRate"] = this.txtTaxRate.Text;
rowNewOrder["TaxAmount"] = this.txtTaxAmount.Text;
rowNewOrder["TotalOrder"] = this.txtTotalOrder.Text;
frmData.dsWorkorders.Tables["Workorder"].Rows.Add(rowNewOrder);
It is typical for a business to sell many products on the same way, just as is usual
for a repair business to perform various transactions on the same day. When
addressing the ability to save work orders, we allowed the user to save many orders
of a typical day in the same file. In the same way, we will allow the user to open a
file that contains all orders placed on a certain day. This would allow the
management to review daily easily.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
7. Return to the form and double-click the Display Orders button
8. Implement the event as follows:
// Because a control can be bound anew every time the user opens a
new set
// Unbind each control to refresh it
txtCustomerName.DataBindings.Clear();
txtAddress.DataBindings.Clear();
txtCity.DataBindings.Clear();
txtState.DataBindings.Clear();
txtZIPCode.DataBindings.Clear();
txtMake.DataBindings.Clear();
txtModel.DataBindings.Clear();
txtCarYear.DataBindings.Clear();
txtProblem.DataBindings.Clear();
txtPartName1.DataBindings.Clear();
txtUnitPrice1.DataBindings.Clear();
txtQuantity1.DataBindings.Clear();
txtSubTotal1.DataBindings.Clear();
txtPartName2.DataBindings.Clear();
txtUnitPrice2.DataBindings.Clear();
txtQuantity2.DataBindings.Clear();
txtSubTotal2.DataBindings.Clear();
txtPartName3.DataBindings.Clear();
txtUnitPrice3.DataBindings.Clear();
txtQuantity3.DataBindings.Clear();
txtSubTotal3.DataBindings.Clear();
txtPartName4.DataBindings.Clear();
txtUnitPrice4.DataBindings.Clear();
txtQuantity4.DataBindings.Clear();
txtSubTotal4.DataBindings.Clear();
txtPartName5.DataBindings.Clear();
txtUnitPrice5.DataBindings.Clear();
txtQuantity5.DataBindings.Clear();
txtSubTotal5.DataBindings.Clear();
txtJobPerformed1.DataBindings.Clear();
txtJobPrice1.DataBindings.Clear();
txtJobPerformed2.DataBindings.Clear();
txtJobPrice2.DataBindings.Clear();
txtJobPerformed3.DataBindings.Clear();
txtJobPrice3.DataBindings.Clear();
txtJobPerformed4.DataBindings.Clear();
txtJobPrice4.DataBindings.Clear();
txtJobPerformed5.DataBindings.Clear();
txtJobPrice5.DataBindings.Clear();
txtRecommendations.DataBindings.Clear();
txtTotalParts.DataBindings.Clear();
txtTotalLabor.DataBindings.Clear();
txtTaxRate.DataBindings.Clear();
txtTaxAmount.DataBindings.Clear();
txtTotalOrder.DataBindings.Clear();
// Bind each control with data gotten from the newly opened file
txtCustomerName.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.CustomerName")); txtAddress.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Addres"));
txtCity.DataBindings.Add(new System.Windows.Forms.Binding("Text",
dsDailyOrders, "Workorder.City"));
txtState.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.State"));
txtZIPCode.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.ZIPCode"));
txtMake.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Make"));
txtModel.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Model"));
txtCarYear.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.CarYear"));
txtProblem.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Problem"));
txtPartName1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.PartName1"));
txtUnitPrice1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.UnitPrice1"));
txtQuantity1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Quantity1"));
txtSubTotal1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.SubTotal1"));
txtPartName2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.PartName2"));
txtUnitPrice2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.UnitPrice2"));
txtQuantity2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Quantity2"));
txtSubTotal2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.SubTotal2"));
txtPartName3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.PartName3"));
txtUnitPrice3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.UnitPrice3"));
txtQuantity3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Quantity3"));
txtSubTotal3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.SubTotal3"));
txtPartName4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.PartName4"));
txtUnitPrice4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.UnitPrice4"));
txtQuantity4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Quantity4"));
txtSubTotal4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.SubTotal4"));
txtPartName5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.PartName5"));
txtUnitPrice5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.UnitPrice5"));
txtQuantity5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Quantity5"));
txtSubTotal5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.SubTotal5"));
txtJobPerformed1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPerformed1"));
txtJobPrice1.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPrice1"));
txtJobPerformed2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPerformed2"));
txtJobPrice2.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPrice2"));
txtJobPerformed3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPerformed3"));
txtJobPrice3.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPrice3"));
txtJobPerformed4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPerformed4"));
txtJobPrice4.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPrice4"));
txtJobPerformed5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPerformed5"));
txtJobPrice5.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.JobPrice5"));
txtRecommendations.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.Recommendation"));
txtTotalParts.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.TotalPart"));
txtTotalLabor.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.TotalLabor"));
txtTaxRate.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.TaxRate"));
txtTaxAmount.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.TaxAmount"));
txtTotalOrder.DataBindings.Add(new
System.Windows.Forms.Binding("Text", dsDailyOrders,
"Workorder.TotalOrder"));
}
else // If there is no data, let the user know
MessageBox.Show(String.Concat("No workorder
available for ", this.dtpOrderDate.Value.ToString()));}}
9. Return to the Workorders form. Double-click the First button and implement
its Click event as follows:
10. Return to the Workorders form. Double-click the Previous button and implement
its Click event as follows:
private void btnPrevious_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsDailyOrders, "Workorder"].Position =
this.BindingContext[dsDailyOrders,
"Workorder"].Position - 1;
}
11. Return to the Workorders form. Double-click the Next button and implement
its Click event as follows: