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 create a data set, in the Toolbox, click the Data button, then click DataSet, and click the form
  3. In the Add Dataset dialog box, click the Untyped dataset radio button
     
    Add Dataset
  4. Click OK
  5. While the new dataSet1 button is selected, in the Properties window, set its (Name) to dsWorkorders and set its DataSetName to Workorders
  6. 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
  7. In the Tables Collection Editor, click Add
  8. Set the (Name) to tblWorkorder and set the TableName to Workorder
  9. Set its Modifiers to Public
  10. To create the columns, in the Workorder Properties section, click Columns and click its ellipsis button
  11. In the Columns Collection Editor, click Add
  12. Set the properties as follows:
    (Name): colCustomerName
    ColumnName: CustomerName
    Modifiers: Public
  13. In the same way, create the other columns as follows:
     
    (Name) ColumnName DataType
    colCustomerName CustomerName System.String
    colAddress Address System.String
    colCity City System.String
    colState State System.String
    colZIPCode ZIPCode System.String
    colMake Make System.String
    colModel Model System.String
    colCarYear CarYear System.String
    colProblem Problem System.String
    colPartName1 PartName1 System.String
    colUnitPrice1 UnitPrice1 System.Double
    colQuantity1 Quantity1 System.Int16
    colSubTotal1 SubTotal1 System.Double
    colPartName2 PartName2 System.String
    colUnitPrice2 UnitPrice2 System.Double
    colQuantity2 Quantity2 System.Int16
    colSubTotal2 SubTotal2 System.Double
    colPartName3 PartName3 System.String
    colUnitPrice3 UnitPrice3 System.Double
    colQuantity3 Quantity3 System.Int16
    colSubTotal3 SubTotal3 System.Double
    colPartName4 PartName4 System.String
    colUnitPrice4 UnitPrice4 System.Double
    colQuantity4 Quantity4 System.Int16
    colSubTotal4 SubTotal4 System.Double
    colPartName5 PartName5 System.String
    colUnitPrice5 UnitPrice5 System.Double
    colQuantity5 Quantity5 System.Int16
    colSubTotal5 SubTotal5 System.Double
    colJobPerformed1 JobPerformed1 System.String
    colJobPrice1 JobPrice1 System.Double
    colJobPerformed2 JobPerformed2 System.String
    colJobPrice2 JobPrice2 System.Double
    colJobPerformed3 JobPerformed3 System.String
    colJobPrice3 JobPrice3 System.Double
    colJobPerformed4 JobPerformed4 System.String
    colJobPrice4 JobPrice4 System.Double
    colJobPerformed5 JobPerformed5 System.String
    colJobPrice5 JobPrice5 System.Double
    colTotalParts TotalParts System.Double
    colTotalLabor TotalLabor System.Double
    colTaxRate TaxRate System.Double
    colTaxAmount TaxAmount System.Double
    colTotalOrder TotalOrder System.Double
    colRecommendations Recommendations System.Double
  14. Click Close twice
  15. 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 btnWorkOrders Previously Processed Repair Orders  
    Button btnClose Close  
    Form     CancelButton: btnClose
  16. Save all
  17. Press Ctrl + A to select everything
  18. Press Ctrl + C to copy everything
  19. To add another form, on the main menu, click Project -> Add Windows Form...
  20. In the Templates list, click Windows Form (.NET)
  21. Set the Name to Workorders and click Open
  22. Enlarge the form as much as possible, then right-click it and click Paste
  23. 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
  24. Display the first form (Form1.vb [Design]) and double-click the Previously Processed Repair Orders button
  25. Implement the Click events as follows:
     
    Private Sub btnWorkOrders_Click(ByVal sender As System.Object, _
    				ByVal e As System.EventArgs) Handles btnWorkOrders.Click
            Dim frmOrders As Workorders = New Workorders
            frmOrders.ShowDialog()
    End Sub
  26. In the Class Name combo box, select btnClose
  27. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, _
    			ByVal e As System.EventArgs) Handles btnClose.Click
            End
    End Sub
  28. 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. In the Class Name combo box, select btnResetOrder
  2. In the Method combo box, select Click the Reset Order button and implement the event as follows:
     
    Private Sub btnResetOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnResetOrder.Click
            Me.txtCustomerName.Text = ""
            Me.txtAddress.Text = ""
            Me.txtCity.Text = ""
            Me.txtState.Text = ""
            Me.txtZIPCode.Text = ""
            Me.txtMake.Text = ""
            Me.txtModel.Text = ""
            Me.txtCarYear.Text = ""
            Me.txtProblem.Text = ""
    
            Me.txtPartName1.Text = ""
            Me.txtUnitPrice1.Text = "0.00"
            Me.txtQuantity1.Text = "0"
            Me.txtSubTotal1.Text = "0.00"
            Me.txtPartName2.Text = ""
            Me.txtUnitPrice2.Text = "0.00"
            Me.txtQuantity2.Text = "0"
            Me.txtSubTotal2.Text = "0.00"
            Me.txtPartName3.Text = ""
            Me.txtUnitPrice3.Text = "0.00"
            Me.txtQuantity3.Text = "0"
            Me.txtSubTotal3.Text = "0.00"
            Me.txtPartName4.Text = ""
            Me.txtUnitPrice4.Text = "0.00"
            Me.txtQuantity4.Text = "0"
            Me.txtSubTotal4.Text = "0.00"
            Me.txtPartName5.Text = ""
            Me.txtUnitPrice5.Text = "0.00"
            Me.txtQuantity5.Text = "0"
            Me.txtSubTotal5.Text = "0.00"
    
            Me.txtJobPerformed1.Text = ""
            Me.txtJobPrice1.Text = "0.00"
            Me.txtJobPerformed2.Text = ""
            Me.txtJobPrice2.Text = "0.00"
            Me.txtJobPerformed3.Text = ""
            Me.txtJobPrice3.Text = "0.00"
            Me.txtJobPerformed4.Text = ""
            Me.txtJobPrice4.Text = "0.00"
            Me.txtJobPerformed5.Text = ""
            Me.txtJobPrice5.Text = "0.00"
    
            Me.txtTotalParts.Text = "0.00"
            Me.txtTotalLabor.Text = "0.00"
            Me.txtTaxRate.Text = "7.75"
            Me.txtTaxAmount.Text = "0.00"
            Me.txtTotalOrder.Text = "0.00"
    
            Me.txtRecommendations.Text = ""
            Me.txtCustomerName.Focus()
        End Sub
  3. In the Class Name combo box, select btnCalculateOrder
  4. In the Method Name combo box, select Click and implement its event as follows:
      
    Private Sub btnCalculateOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculateOrder.Click
            Dim part1UnitPrice As Decimal = 0.0
            Dim part1SubTotal As Decimal = 0.0
            Dim part2UnitPrice As Decimal = 0.0
            Dim part2SubTotal As Decimal = 0.0
            Dim part3UnitPrice As Decimal = 0.0
            Dim part3SubTotal As Decimal = 0.0
            Dim part4UnitPrice As Decimal = 0.0
            Dim part4SubTotal As Decimal = 0.0
            Dim part5UnitPrice As Decimal = 0.0
            Dim part5SubTotal As Decimal = 0.0
            Dim totalParts As Decimal = 0.0
            Dim part1Quantity As Integer = 0
            Dim part2Quantity As Integer = 0
            Dim part3Quantity As Integer = 0
            Dim part4Quantity As Integer = 0
            Dim part5Quantity As Integer = 0
            Dim job1Price As Decimal = 0.0
            Dim job2Price As Decimal = 0.0
            Dim job3Price As Decimal = 0.0
            Dim job4Price As Decimal = 0.0
            Dim job5Price As Decimal = 0.0
            Dim totalLabor As Decimal = 0.0
            Dim taxRate As Decimal = 0.0
            Dim taxAmount As Decimal = 0.0
            Dim totalOrder As Decimal = 0.0
    
            ' Don't charge a part unless it is clearly identified
            If Me.txtPartName1.Text = "" Then
                Me.txtUnitPrice1.Text = "0.00"
                Me.txtQuantity1.Text = "0"
                Me.txtSubTotal1.Text = "0.00"
                part1UnitPrice = 0.0
            Else
                Try
                    part1UnitPrice = Decimal.Parse(Me.txtUnitPrice1.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Unit Price")
                    Me.txtUnitPrice1.Text = "0.00"
                    Me.txtUnitPrice1.Focus()
                End Try
    
                Try
                    part1Quantity = CInt(Me.txtQuantity1.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Quantity")
                    Me.txtQuantity1.Text = "0"
                    Me.txtQuantity1.Focus()
                End Try
    	end if
    
            If Me.txtPartName2.Text = "" Then
                Me.txtUnitPrice2.Text = "0.00"
                Me.txtQuantity2.Text = "0"
                Me.txtSubTotal2.Text = "0.00"
                part2UnitPrice = 0.0
            Else
                Try
                    part2UnitPrice = Decimal.Parse(Me.txtUnitPrice2.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Unit Price")
                    Me.txtUnitPrice2.Text = "0.00"
                    Me.txtUnitPrice2.Focus()
                End Try
    
                Try
                    part2Quantity = CInt(Me.txtQuantity2.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Quantity")
                    Me.txtQuantity2.Text = "0"
                    Me.txtQuantity2.Focus()
                End Try
    	end if
    
            If Me.txtPartName3.Text = "" Then
                Me.txtUnitPrice3.Text = "0.00"
                Me.txtQuantity3.Text = "0"
                Me.txtSubTotal3.Text = "0.00"
                part3UnitPrice = 0.0
            Else
                Try
                    part3UnitPrice = Decimal.Parse(Me.txtUnitPrice3.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Unit Price")
                    Me.txtUnitPrice3.Text = "0.00"
                    Me.txtUnitPrice3.Focus()
                End Try
    
                Try
                    part3Quantity = CInt(Me.txtQuantity3.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Quantity")
                    Me.txtQuantity3.Text = "0"
                    Me.txtQuantity3.Focus()
                End Try
    	end if
    
            If Me.txtPartName4.Text = "" Then
                Me.txtUnitPrice4.Text = "0.00"
                Me.txtQuantity4.Text = "0"
                Me.txtSubTotal4.Text = "0.00"
                part4UnitPrice = 0.0
            Else
                Try
                    part4UnitPrice = Decimal.Parse(Me.txtUnitPrice4.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Unit Price")
                    Me.txtUnitPrice4.Text = "0.00"
                    Me.txtUnitPrice4.Focus()
                End Try
    
                Try
                    part4Quantity = CInt(Me.txtQuantity4.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Quantity")
                    Me.txtQuantity4.Text = "0"
                    Me.txtQuantity4.Focus()
                End Try
            End If
    
            If Me.txtPartName5.Text = "" Then
                Me.txtUnitPrice5.Text = "0.00"
                Me.txtQuantity5.Text = "0"
                Me.txtSubTotal5.Text = "0.00"
                part5UnitPrice = 0.0
            Else
                Try
                    part5UnitPrice = Decimal.Parse(Me.txtUnitPrice5.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Unit Price")
                    Me.txtUnitPrice5.Text = "0.00"
                    Me.txtUnitPrice5.Focus()
                End Try
    
                Try
                    part5Quantity = CInt(Me.txtQuantity5.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Quantity")
                    Me.txtQuantity5.Text = "0"
                    Me.txtQuantity5.Focus()
                End Try
            End If
    
            ' Don't bill the customer for a job that is not specified
            If Me.txtJobPerformed1.Text = "" Then
                Me.txtJobPrice1.Text = "0.00"
                job1Price = 0.0
            Else
                Try
                    job1Price = Decimal.Parse(Me.txtJobPrice1.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Job Price")
                    Me.txtJobPrice1.Text = "0.00"
                    Me.txtJobPrice1.Focus()
                End Try
            End If
    
            If Me.txtJobPerformed2.Text = "" Then
                Me.txtJobPrice2.Text = "0.00"
                job2Price = 0.0
            Else
                Try
                    job2Price = Decimal.Parse(Me.txtJobPrice2.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Job Price")
                    Me.txtJobPrice2.Text = "0.00"
                    Me.txtJobPrice2.Focus()
                End Try
            End If
    
            If Me.txtJobPerformed3.Text = "" Then
                Me.txtJobPrice3.Text = "0.00"
                job3Price = 0.0
            Else
                Try
                    job3Price = Decimal.Parse(Me.txtJobPrice3.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Job Price")
                    Me.txtJobPrice3.Text = "0.00"
                    Me.txtJobPrice3.Focus()
                End Try
            End If
    
            If Me.txtJobPerformed4.Text = "" Then
                Me.txtJobPrice4.Text = "0.00"
                job4Price = 0.0
            Else
                Try
                    job4Price = Decimal.Parse(Me.txtJobPrice4.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Job Price")
                    Me.txtJobPrice4.Text = "0.00"
                    Me.txtJobPrice4.Focus()
                End Try
            End If
    
            If Me.txtJobPerformed5.Text = "" Then
                Me.txtJobPrice5.Text = "0.00"
                job5Price = 0.0
            Else
                Try
                    job5Price = Decimal.Parse(Me.txtJobPrice5.Text)
                Catch ex As FormatException
                    MsgBox("Invalid Job Price")
                    Me.txtJobPrice5.Text = "0.00"
                    Me.txtJobPrice5.Focus()
                End Try
            End If
    
            part1SubTotal = part1UnitPrice * part1Quantity
            part2SubTotal = part2UnitPrice * part2Quantity
            part3SubTotal = part3UnitPrice * part3Quantity
            part4SubTotal = part4UnitPrice * part4Quantity
            part5SubTotal = part5UnitPrice * part5Quantity
    
            Me.txtSubTotal1.Text = part1SubTotal.ToString("F")
            Me.txtSubTotal2.Text = part2SubTotal.ToString("F")
            Me.txtSubTotal3.Text = part3SubTotal.ToString("F")
            Me.txtSubTotal4.Text = part4SubTotal.ToString("F")
            Me.txtSubTotal5.Text = part5SubTotal.ToString("F")
    
            totalParts = part1SubTotal + part2SubTotal + part3SubTotal + _
                         part4SubTotal + part5SubTotal
    
            totalLabor = job1Price + job2Price + job3Price + _
                         job4Price + job5Price
    
            Try
                taxRate = Decimal.Parse(Me.txtTaxRate.Text)
            Catch ex As FormatException
                MsgBox("Invalid Tax Rate")
                Me.txtTaxRate.Text = "7.75"
                Me.txtTaxRate.Focus()
            End Try
    
            Dim totalPartsAndLabor As Decimal = totalParts + totalLabor
            taxAmount = totalPartsAndLabor * taxRate / 100
            totalOrder = totalPartsAndLabor + taxAmount
    
            Me.txtTotalParts.Text = Format(totalParts, "F")
            Me.txtTotalLabor.Text = Format(totalLabor, "F")
            Me.txtTaxAmount.Text = Format(taxAmount, "F")
            Me.txtTotalOrder.Text = Format(totalOrder, "F")
    End Sub
  5. Execute the application to test it and fill the form with a record
  6. Click the Calculate Order button
  7. 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 me. 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. In the top section of the file, type the following:
     
    Imports System.Data
    Imports System.IO
    Imports System.Xml
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
  2. In the Class Name combo box, select btnSaveOrder
  3. In the Method Name combo box, select Click
  4. Implement the event as follows:
     
    Private Sub btnSaveOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
            ' Get the date that the Order Date displays
            Dim tmeToday As DateTime = Me.dtpOrderDate.Value
            Dim day As Integer = tmeToday.Day
            Dim month As Integer = tmeToday.Month
            Dim year As Integer = tmeToday.Year
            Dim strMonth() As String = {"Jan", "Feb", "Mar", "Apr", _
                                        "May", "Jun", "Jul", "Aug", _
                                        "Sep", "Oct", "Nov", "Dec"}
            Dim strFilename As String = CStr(day) & strMonth(month - 1) & _
                                        CStr(year) + ".xml"
    
            ' If the file exists already, open it
            If File.Exists(strFilename) Then
                Me.dsWorkorders.ReadXml(strFilename)
            End If
    
            ' Create a new record for the Workorder table
            Dim rowNewOrder As DataRow = Me.dsWorkorders.Tables("Workorder").NewRow()
    
            rowNewOrder("CustomerName") = Me.txtCustomerName.Text
            rowNewOrder("Address") = Me.txtAddress.Text
            rowNewOrder("City") = Me.txtCity.Text
            rowNewOrder("State") = Me.txtState.Text
            rowNewOrder("ZIPCode") = Me.txtZIPCode.Text
            rowNewOrder("Make") = Me.txtMake.Text
            rowNewOrder("Model") = Me.txtModel.Text
            rowNewOrder("CarYear") = Me.txtCarYear.Text
            rowNewOrder("Problem") = Me.txtProblem.Text
    
            rowNewOrder("PartName1") = Me.txtPartName1.Text
            rowNewOrder("UnitPrice1") = Me.txtUnitPrice1.Text
            rowNewOrder("Quantity1") = Me.txtQuantity1.Text
            rowNewOrder("SubTotal1") = Me.txtSubTotal1.Text
    
            rowNewOrder("PartName2") = Me.txtPartName2.Text
            rowNewOrder("UnitPrice2") = Me.txtUnitPrice2.Text
            rowNewOrder("Quantity2") = Me.txtQuantity2.Text
            rowNewOrder("SubTotal2") = Me.txtSubTotal2.Text
    
            rowNewOrder("PartName3") = Me.txtPartName3.Text
            rowNewOrder("UnitPrice3") = Me.txtUnitPrice3.Text
            rowNewOrder("Quantity3") = Me.txtQuantity3.Text
            rowNewOrder("SubTotal3") = Me.txtSubTotal3.Text
    
            rowNewOrder("PartName4") = Me.txtPartName4.Text
            rowNewOrder("UnitPrice4") = Me.txtUnitPrice4.Text
            rowNewOrder("Quantity4") = Me.txtQuantity4.Text
            rowNewOrder("SubTotal4") = Me.txtSubTotal4.Text
    
            rowNewOrder("PartName5") = Me.txtPartName5.Text
            rowNewOrder("UnitPrice5") = Me.txtUnitPrice5.Text
            rowNewOrder("Quantity5") = Me.txtQuantity5.Text
            rowNewOrder("SubTotal5") = Me.txtSubTotal5.Text
    
            rowNewOrder("JobPerformed1") = Me.txtJobPerformed1.Text
            rowNewOrder("JobPrice1") = Me.txtJobPrice1.Text
            rowNewOrder("JobPerformed2") = Me.txtJobPerformed2.Text
            rowNewOrder("JobPrice2") = Me.txtJobPrice2.Text
            rowNewOrder("JobPerformed3") = Me.txtJobPerformed3.Text
            rowNewOrder("JobPrice3") = Me.txtJobPrice3.Text
            rowNewOrder("JobPerformed4") = Me.txtJobPerformed4.Text
            rowNewOrder("JobPrice4") = Me.txtJobPrice4.Text
            rowNewOrder("JobPerformed5") = Me.txtJobPerformed5.Text
            rowNewOrder("JobPrice5") = Me.txtJobPrice5.Text
    
            rowNewOrder("Recommendations") = Me.txtRecommendations.Text
    
            rowNewOrder("TotalParts") = Me.txtTotalParts.Text
            rowNewOrder("TotalLabor") = Me.txtTotalLabor.Text
            rowNewOrder("TaxRate") = Me.txtTaxRate.Text
            rowNewOrder("TaxAmount") = Me.txtTaxAmount.Text
            rowNewOrder("TotalOrder") = Me.txtTotalOrder.Text
    
            ' Add the new record to the AvailableItems table
            Me.dsWorkorders.Tables("Workorder").Rows.Add(rowNewOrder)
    
            ' Update the XML file
            Me.dsWorkorders.WriteXml(strFilename)
    
            ' Reset the controls in case the user wants to add another record
            Me.btnResetOrder_Click(sender, e)
    End Sub
  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. Right-click the form and click View Code
  3. In the top section of the file, type the following:
     
    Imports System.Data
    Imports System.IO
    Imports System.Xml
    
    Public Class Workorders
        Inherits System.Windows.Forms.Form
  4. In the Class Name combo box, select btnOpen
  5. In the Method Name combo box, select Click
  6. Implement the event as follows:
     
    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            ' Get the date that the Order Date displays
            Dim tmeToday As DateTime = Me.dtpOrderDate.Value
            Dim day As Integer = tmeToday.Day
            Dim month As Integer = tmeToday.Month
            Dim year As Integer = tmeToday.Year
            Dim strMonth() As String = {"Jan", "Feb", "Mar", "Apr", _
                                        "May", "Jun", "Jul", "Aug", _
                                        "Sep", "Oct", "Nov", "Dec"}
            Dim strFilename As String = CStr(day) & strMonth(month - 1) & _
                          CStr(year) & ".xml"
    
            ' If the file exists already, open it
            If File.Exists(strFilename) Then
                 ' 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
                Me.dsWorkorders.Clear()
    
                ' Open the set of orders placed on the day selected
                dsWorkorders.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", dsWorkorders, "Workorder.CustomerName"))
                txtAddress.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Addres"))
                txtCity.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.City"))
                txtState.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.State"))
                txtZIPCode.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.ZIPCode"))
                txtMake.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Make"))
                txtModel.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Model"))
                txtCarYear.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.CarYear"))
                txtProblem.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Problem"))
                txtPartName1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.PartName1"))
                txtUnitPrice1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.UnitPrice1"))
                txtQuantity1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Quantity1"))
                txtSubTotal1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.SubTotal1"))
                txtPartName2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.PartName2"))
                txtUnitPrice2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.UnitPrice2"))
                txtQuantity2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Quantity2"))
                txtSubTotal2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.SubTotal2"))
                txtPartName3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.PartName3"))
                txtUnitPrice3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.UnitPrice3"))
                txtQuantity3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Quantity3"))
                txtSubTotal3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.SubTotal3"))
                txtPartName4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.PartName4"))
                txtUnitPrice4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.UnitPrice4"))
                txtQuantity4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Quantity4"))
                txtSubTotal4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.SubTotal4"))
                txtPartName5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.PartName5"))
                txtUnitPrice5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.UnitPrice5"))
                txtQuantity5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Quantity5"))
                txtSubTotal5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.SubTotal5"))
                txtJobPerformed1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPerformed1"))
                txtJobPrice1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPrice1"))
                txtJobPerformed2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPerformed2"))
                txtJobPrice2.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPrice2"))
                txtJobPerformed3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPerformed3"))
                txtJobPrice3.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPrice3"))
                txtJobPerformed4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPerformed4"))
                txtJobPrice4.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPrice4"))
                txtJobPerformed5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPerformed5"))
                txtJobPrice5.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.JobPrice5"))
                txtRecommendations.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.Recommendation"))
                txtTotalParts.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.TotalPart"))
                txtTotalLabor.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.TotalLabor"))
                txtTaxRate.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.TaxRate"))
                txtTaxAmount.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.TaxAmount"))
                txtTotalOrder.DataBindings.Add(New System.Windows.Forms.Binding("Text", dsWorkorders, "Workorder.TotalOrder"))
            Else ' If there is no data, let the user know
                MsgBox(String.Concat("No workorder available for ", Me.dtpOrderDate.Value.ToString()))
            End If
    End Sub
  7. Execute the application to test it
  8. Close the forms
 

Home Copyright © 2005 FunctionX, Inc.