XML-Based Applications: College Park 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.

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C# and create a new Windows Forms Application named CPAS2
  2. To add another form, on the main menu, click Project -> Add Windows Form...
  3. Set the Name to InfoCenter and click Open
  4. To create a data set, in the Toolbox, click the Data button, then click DataSet, and click the form
  5. In the Add Dataset dialog box, click the Untyped dataset radio button
     
    Add Dataset
  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:
     
    (Name) ColumnName DataType Modifiers
    colCustomerName CustomerName System.String Public
    colAddress Address System.String Public
    colCity City System.String Public
    colState State System.String Public
    colZIPCode ZIPCode System.String Public
    colMake Make System.String Public
    colModel Model System.String Public
    colCarYear CarYear System.String Public
    colProblem Problem System.String Public
    colPartName1 PartName1 System.String Public
    colUnitPrice1 UnitPrice1 System.Double Public
    colQuantity1 Quantity1 System.Int16 Public
    colSubTotal1 SubTotal1 System.Double Public
    colPartName2 PartName2 System.String Public
    colUnitPrice2 UnitPrice2 System.Double Public
    colQuantity2 Quantity2 System.Int16 Public
    colSubTotal2 SubTotal2 System.Double Public
    colPartName3 PartName3 System.String Public
    colUnitPrice3 UnitPrice3 System.Double Public
    colQuantity3 Quantity3 System.Int16 Public
    colSubTotal3 SubTotal3 System.Double Public
    colPartName4 PartName4 System.String Public
    colUnitPrice4 UnitPrice4 System.Double Public
    colQuantity4 Quantity4 System.Int16 Public
    colSubTotal4 SubTotal4 System.Double Public
    colPartName5 PartName5 System.String Public
    colUnitPrice5 UnitPrice5 System.Double Public
    colQuantity5 Quantity5 System.Int16 Public
    colSubTotal5 SubTotal5 System.Double Public
    colJobPerformed1 JobPerformed1 System.String Public
    colJobPrice1 JobPrice1 System.Double Public
    colJobPerformed2 JobPerformed2 System.String Public
    colJobPrice2 JobPrice2 System.Double Public
    colJobPerformed3 JobPerformed3 System.String Public
    colJobPrice3 JobPrice3 System.Double Public
    colJobPerformed4 JobPerformed4 System.String Public
    colJobPrice4 JobPrice4 System.Double Public
    colJobPerformed5 JobPerformed5 System.String Public
    colJobPrice5 JobPrice5 System.Double Public
    colTotalParts TotalParts System.Double Public
    colTotalLabor TotalLabor System.Double Public
    colTaxRate TaxRate System.Double Public
    colTaxAmount TaxAmount System.Double Public
    colTotalOrder TotalOrder System.Double Public
    colRecommendations Recommendations System.Double Public
  16. Click Close twice
  17. To add another form, on the main menu, click Project -> Add Windows Form...
  18. In the Templates list, click Windows Form (.NET)
  19. Set the Name to NewWorkorder and click Open
  20. Design the form as follows:
     
    Control Name Text Other Properties
    GroupBox   Customer and Car Information  
    Label   Customer Name:  
    TextBox txtCustomerName    
    Label   Address  
    TextBox txtAddress    
    Label   City:  
    TextBox txtCity    
    Label   State:  
    TextBox txtState    
    Label   ZIP Code:  
    TextBox txtZIPCode   TextAlign: Right
    Label   Make / Model:  
    TextBox txtMake    
    TextBox txtModel    
    Label   Year:  
    TextBox txtCarYear   TextAlign: Right
    Label   Problem Description:  
    TextBox txtProblem    
    GroupBox   Parts Used  
    Label   Part Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtPartName1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    Enabled: False
    GroupBox   Jobs Performed  
    Label   Price  
    TextBox txtJobPerformed1    
    TextBox txtJobPrice1 0.00 TextAlign: Right
    TextBox txtJobPerformed2    
    TextBox txtJobPrice2 0.00 TextAlign: Right
    TextBox txtJobPerformed3    
    TextBox txtJobPrice3 0.00 TextAlign: Right
    TextBox txtJobPerformed4    
    TextBox txtJobPrice4 0.00 TextAlign: Right
    TextBox txtJobPerformed5    
    TextBox txtJobPrice5 0.00 TextAlign: Right
    GroupBox   Recommendations to Customer  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
    GroupBox   Order Summary  
    Button btnCalculateOrder Calculate Order  
    Label   Total Parts:  
    TextBox txtTotalParts 0.00 TextAlign: Right
    Label   Total Labor:  
    TextBox txtTotalLabor 0.00 TextAlign: Right
    Label   Tax Rate:  
    TextBox txtTaxRate 7.75 TextAlign: Right
    Label   %  
    Label   Tax Amount:  
    TextBox txtTaxAmount 0.00 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Label   Order Date:  
    DateTimePicker dtpOrderDate   Format: Custom
    CustomFormat: ddd dd MMM yyyy
    Button btnSaveOrder Save this Order in the Above Date and Start New One  
    Button btnResetOrder Reset Order  
    Button btnPreview Print Preview  
    Button btnClose Close  
    Form     CancelButton: btnClose
  21. Save all
  22. Press Ctrl + A to select everything
  23. Press Ctrl + C to copy everything
  24. To add another form, on the main menu, click Project -> Add Windows Form...
  25. In the Templates list, click Windows Form (.NET)
  26. Set the Name to Workorders and click Open
  27. Enlarge the form as much as possible, then right-click it and click Paste
  28. Adjust the design of the form as follows:
     
    Control Name Text Other Properties
    GroupBox   Customer and Car Information  
    Label   Customer Name:  
    TextBox txtCustomerName    
    Label   Address  
    TextBox txtAddress    
    Label   City:  
    TextBox txtCity    
    Label   State:  
    TextBox txtState    
    Label   ZIP Code:  
    TextBox txtZIPCode   TextAlign: Right
    Label   Make / Model:  
    TextBox txtMake    
    TextBox txtModel    
    Label   Year:  
    TextBox txtCarYear   TextAlign: Right
    Label   Problem Description:  
    TextBox txtProblem    
    GroupBox   Parts Used  
    Label   Part Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtPartName1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    Enabled: False
    GroupBox   Jobs Performed  
    Label   Price  
    TextBox txtJobPerformed1    
    TextBox txtJobPrice1 0.00 TextAlign: Right
    TextBox txtJobPerformed2    
    TextBox txtJobPrice2 0.00 TextAlign: Right
    TextBox txtJobPerformed3    
    TextBox txtJobPrice3 0.00 TextAlign: Right
    TextBox txtJobPerformed4    
    TextBox txtJobPrice4 0.00 TextAlign: Right
    TextBox txtJobPerformed5    
    TextBox txtJobPrice5 0.00 TextAlign: Right
    GroupBox   Recommendations to Customer  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
    GroupBox   Order Summary  
    Label   Order Date:  
    DateTimePicker dtpOrderDate   Format: Custom
    CustomFormat: ddd dd MMM yyyy
    Button btnOpen Display Orders  
    Label   Total Parts:  
    TextBox txtTotalParts 0.00 TextAlign: Right
    Label   Total Labor:  
    TextBox txtTotalLabor 0.00 TextAlign: Right
    Label   Tax Rate:  
    TextBox txtTaxRate 7.75 TextAlign: Right
    Label   %  
    Label   Tax Amount:  
    TextBox txtTaxAmount 0.00 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Button btnFirst First  
    Button btnNext Next  
    Button btnPrevious Previous  
    Button btnLast Last  
    Button btnPreview Print Preview  
    Button btnClose Close  
    Form     CancelButton: btnClose
  29. Save all
  30. Display the first or main form (Form1.h [Design]) and design it as follows:
     
    Control Name Text
    Button btnNewOrder Create New Workorder
    Button btnOpenOrders Open Previous Workorders
    Button btnClose Close
  31. Double-click the Create New Workorder button
  32. Return to the form and double-click the Open Previous Workorders button
  33. Return to the form and double-click the Close button
  34. Implement the Click events as follows:
     
    private void btnNewOrder_Click(object sender, System.EventArgs e)
    {
    	NewWorkorder frmOrder = new NewWorkorder();
    	frmOrder.ShowDialog();
    }
    
    private void btnOpenOrders_Click(object sender, System.EventArgs e)
    {
    	Workorders frmOrders = new Workorders();
    	frmOrders.ShowDialog();
    }
    
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  35. Execute the application to test it
 
 

Calculating the Order

When a car has been fixed, an employee can complete the form with what was done. A section of the form allows the employee to enter the names, prices, and quantities of parts that were used to fix the car. Each part is entered but its name, a unit price, and a quantity; then, a sub total of each part must be calculated.

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:
     
    private void btnResetOrder_Click(object sender, System.EventArgs e)
    {
    	this.txtCustomerName.Text = "";
    	this.txtAddress.Text = "";
    	this.txtCity.Text = "";
    	this.txtState.Text = "";
    	this.txtZIPCode.Text = "";
    	this.txtMake.Text = "";
    	this.txtModel.Text = "";
    	this.txtCarYear.Text = "";
    	this.txtProblem.Text = "";
    
    	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:
      
    private void btnCalculateOrder_Click(object sender, System.EventArgs e)
    {
    	decimal part1UnitPrice = 0.00M,
                                 part1SubTotal  = 0.00M,
                                 part2UnitPrice = 0.00M,
                                 part2SubTotal  = 0.00M,
                                 part3UnitPrice = 0.00M,
                                 part3SubTotal  = 0.00M,
    	             part4UnitPrice = 0.00M,
    	             part4SubTotal  = 0.00M,
    		    part5UnitPrice = 0.00M,
    		    part5SubTotal  = 0.00M,
    		    totalParts     = 0.00M;
    	int    part1Quantity = 0, part2Quantity = 0, part3Quantity = 0,
    		   part4Quantity = 0, part5Quantity = 0;
    	decimal job1Price = 0.00M,
    		    job2Price = 0.00M,
    		    job3Price = 0.00M,
    		    job4Price = 0.00M,
    		    job5Price = 0.00M;
    	decimal totalLabor = 0.00M;
    	decimal taxRate = 0.00M, taxAmount = 0.00M, totalOrder = 0.00M;
    
    	// Don't charge a part unless it is clearly identified
    	if( this.txtPartName1.Text == "" )
    	{
    		this.txtUnitPrice1.Text = "0.00";
    		this.txtQuantity1.Text  = "0";
    		this.txtSubTotal1.Text  = "0.00";
    		part1UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part1UnitPrice = decimal.Parse(this.txtUnitPrice1.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice1.Text = "0.00";
    			this.txtUnitPrice1.Focus();
    		}
    
    		try 
    		{
    			part1Quantity = int.Parse(this.txtQuantity1.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity1.Text = "0";
    			this.txtQuantity1.Focus();
    		}
    	}
    
    	if( this.txtPartName2.Text == "" )
    	{
    		this.txtUnitPrice2.Text = "0.00";
    		this.txtQuantity2.Text  = "0";
    		this.txtSubTotal2.Text  = "0.00";
    		part2UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part2UnitPrice = decimal.Parse(this.txtUnitPrice2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice2.Text = "0.00";
    			this.txtUnitPrice2.Focus();
    		}
    
    		try 
    		{
    			part2Quantity = int.Parse(this.txtQuantity2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity2.Text = "0";
    			this.txtQuantity2.Focus();
    		}
    	}
    
    	if( this.txtPartName3.Text == "" )
    	{
    		this.txtUnitPrice3.Text = "0.00";
    		this.txtQuantity3.Text  = "0";
    		this.txtSubTotal3.Text  = "0.00";
    		part3UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part3UnitPrice = decimal.Parse(this.txtUnitPrice3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice3.Text = "0.00";
    			this.txtUnitPrice3.Focus();
    		}
    
    		try 
    		{
    			part3Quantity = int.Parse(this.txtQuantity3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity3.Text = "0";
    			this.txtQuantity3.Focus();
    		}
    	}
    
    	if( this.txtPartName4.Text == "" )
    	{
    		this.txtUnitPrice4.Text = "0.00";
    		this.txtQuantity4.Text  = "0";
    		this.txtSubTotal4.Text  = "0.00";
    		part4UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part4UnitPrice = decimal.Parse(this.txtUnitPrice4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice4.Text = "0.00";
    			this.txtUnitPrice4.Focus();
    		}
    
    		try 
    		{
    			part4Quantity = int.Parse(this.txtQuantity4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity4.Text = "0";
    			this.txtQuantity4.Focus();
    		}
    	}
    
    	if( this.txtPartName5.Text == "" )
    	{
    		this.txtUnitPrice5.Text = "0.00";
    		this.txtQuantity5.Text  = "0";
    		this.txtSubTotal5.Text  = "0.00";
    		part5UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part5UnitPrice = decimal.Parse(this.txtUnitPrice5.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice5.Text = "0.00";
    			this.txtUnitPrice5.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();
    		}
    	}
    				 
    	if( this.txtJobPerformed2.Text == "" )
    	{
    		this.txtJobPrice2.Text = "0.00";
    		job2Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job2Price = decimal.Parse(this.txtJobPrice2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice2.Text = "0.00";
    			this.txtJobPrice2.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed3.Text == "" )
    	{
    		this.txtJobPrice3.Text = "0.00";
    		job3Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job3Price = decimal.Parse(this.txtJobPrice3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice3.Text = "0.00";
    			this.txtJobPrice3.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed4.Text == "" )
    	{
    		this.txtJobPrice4.Text = "0.00";
    		job4Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job4Price = decimal.Parse(this.txtJobPrice4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice4.Text = "0.00";
    			this.txtJobPrice4.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed5.Text == "" )
    	{
    		this.txtJobPrice5.Text = "0.00";
    		job5Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job5Price = decimal.Parse(this.txtJobPrice5.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice5.Text = "0.00";
    			this.txtJobPrice5.Focus();
    		}
    	}
    				 
    	part1SubTotal = part1UnitPrice * part1Quantity;
    	part2SubTotal = part2UnitPrice * part2Quantity;
    	part3SubTotal = part3UnitPrice * part3Quantity;
    	part4SubTotal = part4UnitPrice * part4Quantity;
    	part5SubTotal = part5UnitPrice * part5Quantity;
    
    	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");
    
    	totalParts    = part1SubTotal + part2SubTotal + part3SubTotal +
    		part4SubTotal + part5SubTotal;
    				 
    	totalLabor    = job1Price + job2Price + job3Price +
    		job4Price + job5Price;
    				 
    	try 
    	{
    		taxRate = decimal.Parse(this.txtTaxRate.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Tax Rate");
    		this.txtTaxRate.Text = "7.75";
    		this.txtTaxRate.Focus();
    	}
    				 
    	decimal totalPartsAndLabor = totalParts + totalLabor;
    	taxAmount  = totalPartsAndLabor * taxRate / 100;
    	totalOrder = totalPartsAndLabor + taxAmount;
    				 
    	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
 

Saving the Customer Order

Once a work order has been completed, it can be saved. There are various ways you can solve this. One of the easiest ways would consist of saving each order directly in its own file. The problem is that this would create too many files, especially if this business is successful. Another easy solution would consist of directly saving all orders of one customer in a particular file. This is suitable if the business deals with regularly returning customers, which is not always the case. Overall, you should provide as much flexibility as possible.

To make our application useful, we will allow the user to save an order to a file that whose name is after the day this order was placed. We will prompt the user so he or she can change the name of the file if necessary. When saving an order, if there is no order with that name, we will create a new XML file and store the order in it. If a file already exists under that name, then we will add the current order as part of the file. That way, all orders of a certain date can be saved in the same file, making it possible to retrieve the list of repairs that were completed on a certain day.

Practical Learning: Saving a Work Order

  1. Display the New Workorder form (NewWorkorder.cs [Design]))
  2. Double-click the Save this Order button
  3. In the top section of the file, under the other using lines, type the following:
     
    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";
    
    			// Get a reference to the Data Center because 
    			// that's where the DataSet object resides
    			InfoCenter frmData = new InfoCenter();
    
    			// If the file exists already, open it
    			if( File.Exists(strFilename) )
    				frmData.dsWorkorders.ReadXml(strFilename);
    
    			// Create a new record for the Workorder table
    			DataRow rowNewOrder = frmData.dsWorkorders.Tables["Workorder"].NewRow();
    
    			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;
    
    			// Add the new record to the AvailableItems table
    			frmData.dsWorkorders.Tables["Workorder"].Rows.Add(rowNewOrder);
    
    			// Update the XML file
    			frmData.dsWorkorders.WriteXml(strFilename);
    
    			// Reset the controls in case the user wants to add another record
    			this.btnResetOrder_Click(sender, e);
    		}
    	}
  5. Execute the application to test it
  6. Create a new record then click Calculate Order and save the order
  7. Create another order, calculate it and save it
  8. Close the form and return to your programming environment
 
 

Opening Customers' Orders

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.

Practical Learning: Opening and Reviewing Daily Orders

  1. Display the Workorders form
  2. In the Data section of the Toolbox, click DataSet and click the form
  3. In the Add Dataset dialog box, click the Untyped Dataset radio button and click OK
  4. Change its properties as follows:
    (Name): dsDailyOrders
    DataSetName: DailyOrders
  5. Right-click the form and click View Code
  6. In the top section of the file, type the following:
     
    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:
     
    private void btnOpen_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";
    
    	 // If the file exists already, open it
    	 if( File.Exists(strFilename) )
    	 {
    		 // Get a reference to the object that holds the daily repairs
    		 InfoCenter frmInfo = new InfoCenter();
    		 // Empty the local data set of any data. This is required if
    		 // we want the user to be able to open different daily sets of repairs
    		 this.dsDailyOrders.Clear();
    		 // Refresh or initialize the local set with the information
    		 // stored centrally
    		 this.dsDailyOrders = frmInfo.dsWorkorders;
    
    		 // Open the set of orders placed on the day selected
    		 dsDailyOrders.ReadXml(strFilename);
    
    		 // 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:
     
    private void btnFirst_Click(object sender, System.EventArgs e)
    {
    	this.BindingContext[dsDailyOrders, "Workorder"].Position = 0;
    }
  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:
     
    private void btnNext_Click(object sender, System.EventArgs e)
    {
    	this.BindingContext[dsDailyOrders, "Workorder"].Position = 
    		this.BindingContext[dsDailyOrders, "Workorder"].Position + 1;
    }
  12. Return to the Workorders form. Double-click the Last button and implement its Click event as follows:
     
    private void btnLast_Click(object sender, System.EventArgs e)
    {
    	this.BindingContext[dsDailyOrders, "Workorder"].Position = 
    		this.BindingContext[dsDailyOrders, "Workorder"].Count - 1;
    }
  13. Display the Workorders form and try opening a previously saved order
  14. Close the forms
 

Home Copyright © 2004-2014 FunctionX, Inc.