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 New Item...
  38. In the Templates list, click Windows Form and set the name to OrderProcessing
  39. Click Add
  40. Enlarge and heighten the form
  41. In the Data Sources window, click OrderProcessing and click the arrow of its combo box
  42. Select Details
  43. From the Data Sources window, drag OrderProcessing and drop it on the form
  44. 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)
  45. Once again, from the Data Sources window, drag Employees and drop it on the form
  46. While the DataGridView control is still selected on the form, press Delete to remove it from the form
  47. Once again, from the Data Sources window, drag Customers and drop it in an empty area of the form
  48. While the DataGridView control is still selected on the form, press Delete to remove it from the form
  49. Once again, from the Data Sources window, drag Cars and drop it on the form
  50. While the new controls are still selected on the form, press Delete to remove them from the form
  51. Once again, from the Data Sources window, drag EmployeesNames and drop it on the form
  52. While the DataGridView control is still selected on the form, press Delete to remove it
  53. Delete the text boxes of OrderDate, OrderTime, EmployeeID, CustomerID, CarID, CarCondition, TankLevel, StartDate, and EndDate:
  54. 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 btnCarDetails  
    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 Tax Amount:    
    TextBox     Text: 0.00
    TextAlign: Right
    Label Rent Total:    
    TextBox     Text: 0.00
    TextAlign: Right
    Button Close btnClose  
  55. Click each control that was added and change its data bindings
  56. On the form, double-click the Details button in the Cars section
  57. In the top section of the file, under the #pragma once line, type #include "CarDetails.h"
  58. Scroll to the bottom of the file and implement the event as follows:
     
    System::Void btnCarDetails_Click(System::Object^  sender, 
    				System::EventArgs^  e)
    {
        if( cboCarID->Text->Equals(L"") )
    	MessageBox::Show(L"No car is currently selected");
        else
        {
    	 CarDetails ^ frmCarDetails = gcnew CarDetails;
    	 String ^ strCar = 
    		String::Concat(L"SELECT * FROM Cars WHERE TagNumber = '",
    					 cboCarID->Text, L"'");
    	 System::Data::SqlClient::SqlConnection ^ conCar = 
    		gcnew System::Data::SqlClient::SqlConnection(
    	L"Data Source=(local);Database=bcr1;Integrated Security=yes");
    	 System::Data::SqlClient::SqlCommand ^ cmdCar = 
    		gcnew 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 = true;
             frmCarDetails->btnSubmit->Visible = true;
    	 frmCarDetails->ShowDialog();
    	 conCar->Close();
        }
    }
  59. Return to the OrderProcessing form, click the text box on the right side of Number Of Days
  60. In the Properties window, click the Events box and generate the event of the Leave field
  61. Define it as follows:
     
    System::Void NumberOfDaysTextBox_Leave(System::Object^  sender,
    					System::EventArgs^  e)
    {
        int numberOfDays;
        double rateApplied, subTotal;
    
        try {
    	 rateApplied = double::Parse(this->RateAppliedTextBox->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the rate applied");
        }
    
        try {
    	 numberOfDays = int::Parse(this->NumberOfDaysTextBox->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the number of days");
        }
    
        subTotal = rateApplied * numberOfDays;
        this->SubTotalTextBox->Text = subTotal.ToString(L"F");
    }
  62. Return to the OrderProcessing form, click the text box on the right side of Tax Rate
  63. In the Events section of the Properties window, generate the event of the Leave field
  64. Define it as follows:
     
    System::Void TaxRateTextBox_Leave(System::Object^  sender,
    				  System::EventArgs^  e)
    {
        double subTotal, taxRate, taxAmount, rentTotal;
    			 
        try {
    	 subTotal = double::Parse(this->SubTotalTextBox->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for sub-total");
        }
    
        try {
    	 taxRate = double::Parse(TaxRateTextBox->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the tax rate");
        }
    
        taxAmount = subTotal * taxRate / 100;
        rentTotal = subTotal + taxAmount;
    
        this->TaxAmountTextBox->Text = taxAmount.ToString(L"F");
        this->RentTotalTextBox->Text = rentTotal.ToString(L"F");
    }
  65. Double-click the Close button and implement its even as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
         Close();
    }
  66. Access the first form
  67. Add a button to it and set its properties as follows:
    Text: OrderProcessing
    (Name): btnOrderProcessing
  68. Double-click the Customers button
  69. In the top section of the file, under the #pragma once line, type
    #include "OrderProcessing.h"
  70. Scroll to the bottom of the file and implement the even as follows:
     
    System::Void btnOrderProcessing_Click(System::Object^  sender,
    					System::EventArgs^  e)
    {
    	 Order1 ^ frmOrder1 = gcnew Order1;
    	 frmOrder1->ShowDialog();
    }
  71. Execute the application to test it
  72. Display the OrderProcessing form
  73. To process an order, click the + button and complete the form
     
  74. After completing the form, click the Save button
  75. Close the form(s)
 

Previous Copyright © 2006 FunctionX, Inc.