Home

ADO.NET Application: Bethesda Car Rental

 

Order Processing

After creating the accessory objects of a database, you can get the application to perform the actual transactions. For a car rental company, we will create the object used to process the regular rental transactions. 

Practical Learning: Introducing the Application

  1. In the Microsoft SQL Server Management Studio, if necessary, expand the Databases, the bcr, and the Tables node.
    To create a new table, right the Tables node under bcr and click New Table...
  2. Set the first column name to OrderProcessingID
  3. Set its Data Type to int
  4. In the lower section, expand Identity Specification and set the (Is Identify) field to Yes
  5. On the Table Designer toolbar, click the Set Primary Key button
  6. Complete the table with the following columns
     
    Column Name Data Type Allow Nulls
    OrderProcessingID int  
    OrderDate varchar(50)  
    OrderTime varchar(50)  
    EmployeeID int Uncheck
    CustomerID int Uncheck
    CarID int Uncheck
    CarCondition varchar(20)  
    TankLevel varchar(50)  
    StartDate varchar(50) Uncheck
    EndDate varchar(50)  
    NumberOfDays varchar(50)  
    RateApplied varchar(50)  
    SubTotal varchar(50)  
    TaxRate varchar(50)  
    TaxAmount varchar(50)  
    RentTotal varchar(50)  
    Notes varchar(255)  
  7. Save the table as OrderProcessing
  8. Right-click the table and click Relationships...
  9. Click Add
  10. Click Tables and Columns Specification and click its ellipsis button
  11. Under Primary Key Table, select Employees and, under Employees, select EmployeeID
  12. Under OrderProcessing, select EmployeeID
  13. Click OK
  14. Click Add again
  15. Click the ellipsis button of Tables and Columns Specification
  16. Under Primary Key Table, select Customers and, under Employees, select CustomerID
  17. Under OrderProcessing, select CustomerID
  18. Click OK
  19. Click Add
  20. Click the ellipsis button of Tables and Columns Specification
  21. Under Primary Key Table, select Cars and, under Cars, select CarID
  22. Click OK
  23. Click Close
  24. Close the table
  25. When asked whether you want to save it, click Yes and Yes
  26. To create a view, in the Object Explorer under bcr, right-click Views and click New View...
  27. In the Add Table dialog box, click Employees
  28. Click Add and click Close
  29. In the Diagram pane (the top section), click the check box of EmployeeID
  30. In the SQL pane, on the right side of SELECT EmployeeID, add
    LastName + ', ' + FirstName AS FullName
  31. Save the view as EmployeesNames
     
  32. Close it
  33. In Microsoft Visual Studio, if necessary, open the bcr1 application created in the first section
    In the Data Source window, right-click dsBCR and click Configure DataSet With Wizard...
  34. Expand the Tables node and click the check box of OrderProcessing
  35. Expand the Views node and click the check box of EmployeesNames
     
  36. Click Finish
  37. To create a new form, on the main menu, click Project . Add Windows Form...
  38. Set the name to OrderProcessing and click Add
  39. Enlarge and heighten the form
  40. In the Data Sources window, click OrderProcessing and click the arrow of its combo box
  41. Select Details
  42. From the Data Sources window, drag OrderProcessing and drop it on the form
  43. While the controls are still selected, move them to the left side of the form (to create an empty space in the middle and on the right side of the form)
  44. Again, from the Data Sources window, drag Employees and drop it on the form
  45. While the DataGridView control is still selected on the form, press Delete to remove it from the form
  46. Once again, from the Data Sources window, drag Customers and drop it in an empty area of the form
  47. While the DataGridView control is still selected on the form, press Delete to remove it from the form
  48. Once again, from the Data Sources window, drag Cars and drop it on the form
  49. While the new controls are still selected on the form, press Delete to remove them from the form
  50. Once again, from the Data Sources window, drag EmployeesNames and drop it on the form
  51. While the DataGridView control is still selected on the form, press Delete to remove it
  52. Delete the text boxes of OrderDate, OrderTime, EmployeeID, CustomerID, CarID, CarCondition, TankLevel, StartDate, and EndDate
  53. Design the form as follows (you will add new controls to replace those that were deleted):
     
    Order Processing
    Control Text Name Additional Properties
    Label Processed By:    
    ComboBox   cboEmployeeID  
    Label Order Processing ID:    
    TextBox      
    Label Order Date:    
    DateTimePicker   dtpOrderDate  
    Label Order Time:    
    DateTimePicker   dtpOrderTime Format: Time
    ShowUpDown: True
    GroupBox Customer    
    Label Select:    
    ComboBox cboCustomerID    
    Label Name:    
    TextBox   txtCustomerName  
    Label Address:    
    TextBox   txtCustomerAddress  
    TextBox   txtCustomerCity  
    TextBox   txtCustomerState  
    TextBox   txtCustomerZIPCode  
    TextBox   txtCustomerCountry  
    GroupBox Car    
    Label Select:    
    ComboBox cboCarID    
    Button Details btnCar Details  
    Label Make:    
    TextBox   txtCarMake  
    Label Model:    
    TextBox   txtCarModel  
    Label Year:    
    TextBox   txtCarYear  
    Label Mileage:    
    TextBox   txtCarMileage  
    Label Car Condition:    
    ComboBox   cboCarConditions Items:
    Excellent
    Good
    Driveable
    Needs Repair
    Label Tank Level:    
    ComboBox   cboTankLevels Items:
    Empty
    1/4 Empty
    Half
    3/4 Full
    Full Tank
    Label Start Date:    
    DateTimePicker   dtpStartRent  
    Label End Time:    
    DateTimePicker   dtpEndRent  
    Label Notes:    
    TextBox     Multiline: True
    ScrollBars: Vertical
    Label Rate Applied:    
    TextBox     Text: 0.00
    TextAlign: Right
    Label Number Of Days:    
    TextBox     Text: 0
    TextAlign: Right
    Label Sub Total:    
    TextBox     Text: 0.00
    TextAlign: Right
    Label Tax Rate:    
    TextBox     Text: 5.75
    TextAlign: Right
    Label %    
    Label Tax Amount:    
    TextBox     Text: 0.00
    TextAlign: Right
    Label Rent Total:    
    TextBox     Text: 0.00
    TextAlign: Right
    Button Close btnClose  
  54. Click each control that was added and change its data bindings
  55. On the form, double-click the Details button in the Cars section and implement its Click event as follows:
    private void btnCarDetails_Click(object sender, EventArgs e)
    {
        if (cboCarID.Text == "")
            MessageBox.Show("No car is currently selected");
        else
        {
            CarDetails frmCarDetails = new CarDetails();
            string strCar =
                       string.Concat("SELECT * FROM Cars WHERE TagNumber = '",
                                    cboCarID.Text, "'");
            System.Data.SqlClient.SqlConnection conCar =
                       new System.Data.SqlClient.SqlConnection(
                   "Data Source=(local);Database=bcr;Integrated Security=yes");
                    System.Data.SqlClient.SqlCommand cmdCar =
                       new System.Data.SqlClient.SqlCommand(strCar, conCar);
                    conCar.Open();
    
            System.Data.SqlClient.SqlDataReader rdrCar = cmdCar.ExecuteReader();
    
            while (rdrCar.Read())
            {
                frmCarDetails.txtTagNumber.Text = rdrCar.GetString(1);
                frmCarDetails.txtMake.Text = rdrCar.GetString(2);
                frmCarDetails.txtModel.Text = rdrCar.GetString(3);
                frmCarDetails.txtYear.Text = rdrCar.GetInt32(4).ToString();
                frmCarDetails.txtMileage.Text = rdrCar.GetInt32(5).ToString();
                frmCarDetails.cboCategory.Text = rdrCar.GetString(6);
                frmCarDetails.txtPictureName.Text = rdrCar.GetString(7);
                frmCarDetails.chkK7Player.Checked = rdrCar.GetBoolean(8);
                frmCarDetails.chkDVDPlayer.Checked = rdrCar.GetBoolean(9);
                frmCarDetails.chkCDPlayer.Checked = rdrCar.GetBoolean(10);
                frmCarDetails.chkAvailable.Checked = rdrCar.GetBoolean(11);
                frmCarDetails.pctCar.Image =
                	Image.FromFile(frmCarDetails.txtPictureName.Text);
            }
    
            frmCarDetails.btnPicture.Visible = false;
            frmCarDetails.btnSubmit.Visible = false;
            frmCarDetails.ShowDialog();
            conCar.Close();
        }
    }
  56. Return to the OrderProcessing form, click the text box on the right side of Number Of Days
  57. In the Properties window, click the Events box and generate the event of the Leave field
  58. Define it as follows:
    private void numberOfDaysTextBox_Leave(object sender, EventArgs e)
    {
        int numberOfDays = 0;
        decimal rateApplied = 0.00M, subTotal = 0.00M;
    
        try {
    	 rateApplied = decimal.Parse(rateAppliedTextBox.Text);
        }
        catch(FormatException )
        {
    	 MessageBox.Show("Invalid value for the rate applied");
        }
    
        try {
    	 numberOfDays = int.Parse(numberOfDaysTextBox.Text);
        }
        catch(FormatException )
        {
    	 MessageBox.Show("Invalid value for the number of days");
        }
    
        subTotal = rateApplied * numberOfDays;
        subTotalTextBox.Text = subTotal.ToString("F");
    }
  59. Return to OrderProcessing the form, click the text box on the right side of Tax Rate
  60. In the Events section of the Properties window, generate the event of the Leave field
  61. Define it as follows:
    private void taxRateTextBox_Leave(object sender, EventArgs e)
    {
        decimal subTotal = 0.00M, taxRate = 0.00M,
                taxAmount = 0.00M, rentTotal = 0.00M;
    			 
        try {
            subTotal = decimal.Parse(subTotalTextBox.Text);
        }
        catch(FormatException )
        {
    	 MessageBox.Show("Invalid value for sub-tota");
        }
    
        try {
            taxRate = decimal.Parse(taxRateTextBox.Text);
        }
        catch(FormatException )
        {
    	 MessageBox.Show("Invalid value for the tax rate");
        }
    
        taxAmount = subTotal * taxRate / 100;
        rentTotal = subTotal + taxAmount;
    
        taxAmountTextBox.Text = taxAmount.ToString("F");
        rentTotalTextBox.Text = rentTotal.ToString("F");
    }
  62. Return to OrderProcessing the form and double-click the Close button
  63. Implement its even as follows:
    private void btnClose_Click(object  sender, EventArgs  e)
    {
    	 Close();
    }
  64. Access the Central form
  65. Add a button to it and set its properties as follows:
    Text: OrderProcessing
    (Name): btnOrderProcessing
  66. Double-click the Customers button
  67. In the top section of the file, under the #pragma once line, type
    #include "OrderProcessing.h"
  68. Scroll to the bottom of the file and implement the even as follows:
    private void btnOrderProcessing_Click(object sender, EventArgs e)
    {
        OrderProcessing frmOrder = new OrderProcessing();
        frmOrder.ShowDialog();
    }
  69. Execute the application to test it
  70. Display the OrderProcessing form
  71. To process an order, click the + button and complete the form
     
  72. After completing the form, click the Save button
  73. Close the form(s)
 

Previous Copyright © 2006-2008, FunctionX, Inc.