Home

ADO.NET Application: Bethesda Car Rental

 

Cars

A car is the object that a customer comes to rent to a car rental company. Once again, you should have a list of the cars that the company owns. There are usually two categories of information needed for a car. Basic information is needed when processing a rental order. On the other hand, at times, a customer may want some extended information about a car he or she is renting. Based on these, we will create one table of cars by different forms to access its information, depending on the task at hand.

Practical Learning Practical Learning: Introducing the Application

  1. Save the following cars to a folder. This application assumes that you would have saved them to a folder named Cars inside of a folder name Programs in a computer named Training (and that the Programs folder is shared)
     
     
  2. 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...
  3. Set the first column name to CarID
  4. Set its Data Type to int
  5. In the lower section, expand Identity Specification and set the (Is Identify) field to Yes
  6. On the Table Designer toolbar, click the Set Primary Key button
  7. Complete the table with the following columns
     
    Column Name Data Type Allow Nulls
    CarID int  
    TagNumber varchar(50) Uncheck
    Make varchar(50) Uncheck
    Model varchar(50) Uncheck
    CarYear int  
    Mileage int  
    Category varchar(50)  
    PictureName varchar(200)  
    K7Player bit  
    DVDPlayer bit  
    CDPlayer bit  
    Available bit Uncheck
  8. Save the table as Cars and close it
  9. 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...
  10. Expand the Tables node and click the check box of Cars
  11. Click Finish
  12. To create a new form, on the main menu, click Project -> Add New Item...
  13. In the Templates list, click Windows Form and set the name to CarDetails
  14. Click Add
  15. Design the form as follows: 
     
    Control Text Name Modifiers Additional Properties
    Label Tag Number:      
    TextBox   txtTagNumber Public  
    Label Make:      
    TextBox   txtMake Public  
    Label Model:      
    TextBox   txtModel Public  
    Label Year:      
    TextBox   txtYear Public AlignText: Right
    Label Mileage      
    TextBox   txtMileage Public AlignText: Right
    Label Category:      
    ComboBox   cboCategory Public Items:
    Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Van
    CheckBox K7 Player chkK7Player Public  
    CheckBox DVD Player chkDVDPlayer Public  
    CheckBox CD Player chkCDPlayer Public  
    CheckBox Available chkAvailable Public  
    PictureBox   pctCar   Image: none.gif
    TextBox \\Training\Programs\Cars\none.gif txtPictureName Public  
    Button Picture... chkPicture   Modifiers: Public
    Button Submit btnSubmit   DialogResult: OK
    Modifiers: Public
    Button Close btnClose    
  16. To create a new form, on the main menu, click Project -> Add New Item...
  17. In the Templates list, click Windows Form and set the name to CarsRecords
  18. Click Add
  19. Design the form as follows: 
     
    Control Text Name Additional Properties
    Label Car ID:    
    TextBox   txtCarID  
    Label Tag Number:    
    TextBox   txtTagNumber  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear AlignText: Right
    Label Mileage    
    TextBox   txtMileage AlignText: Right
    Label Category:    
    ComboBox   cboCategory Items:
    Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Van
    CheckBox K7 Player chkK7Player  
    CheckBox DVD Player chkDVDPlayer  
    CheckBox CD Player chkCDPlayer  
    CheckBox Available chkAvailable  
    PictureBox   pctCar Image: none.gif
    TextBox \\Training\Programs\Cars\none.gif txtPictureName  
    Button Picture... chkPicture Modifiers: Public
    Button Close btnClose  
    Button New Car btnNewCar  
    Button First btnFirst  
    Button Previous btnPrevious  
    Button Next btnNext  
    Button Last btnLast  
  20. Return to the form
  21. On the Toolbox, click DataSet In the Data section) and click the form
  22. In the Add Dataset dialog box, accept the Typed dataset radio button and the contents of the Name combo box. Click OK
  23. In the (Data section of the) Toolbox, click BindingSource and click the form
  24. In the Properties window, change its characteristics as follows:
    Name: bsCars
    DataSource: dsBCR1
    DataMember: Cars
  25. Click each control and set its data bindings
  26. On the Toolbox, click the OpenFileDialog button and click the form
  27. Set its name to dlgPicture
  28. Set its Filter to
    Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*
  29. Double-click the Picture button and implement its event as follows:
     
    System::Void btnPicture_Click(System::Object^  sender,
    				System::EventArgs^  e)
    {
        if( dlgPicture->ShowDialog() == ::DialogResult::OK )
        {
    	 this->txtPictureName->Text = dlgPicture->FileName;
    	 this->pctCar->Image = Image::FromFile(txtPictureName->Text);
        }
    }
  30. Return to the form
  31. Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
         Close();
    }
  32. Return to the form
  33. Double-click the New Car button
  34. In the top section of the file, under the #pragma once line, type #include "CarDetails.h"
  35. Scroll to the bottom of the file and implement the event as follows:
     
    System::Void btnNewCar_Click(System::Object^  sender, System::EventArgs^  e)
    {
        CarDetails ^ dlgCar = gcnew CarDetails;
    
        dlgCar->btnSubmit->Visible = true;
    
        if( dlgCar->ShowDialog() == ::DialogResult::OK )
        {
    	if( dlgCar->txtTagNumber->Text->Equals(L"") )
    	{
    	 MessageBox::Show(L"You must specify the tag number of the car\n"
    		              L"The record will not be created");
    		 return;
    	}
    	if( dlgCar->txtMake->Text->Equals(L"") )
    	{
    		 MessageBox::Show(L"You must specify the car make\n"
    		              L"The record will not be created");
    		 return;
    	}
    	if( dlgCar->txtModel->Text->Equals(L"") )
    	{
    		 MessageBox::Show(L"You must specify the car model\n"
    			              L"The record will not be created");
    		 return;
    	}
    	if( dlgCar->txtYear->Text->Equals(L"") )
    	{
    		 MessageBox::Show(L"You must specify the year of the car\n"
    			              L"The record will not be created");
    		 return;
    	}
    
    String ^ strNewCar = String::Concat(L"INSERT INTO Cars(TagNumber, Make, "
                                     L"Model, CarYear, Mileage, Category, "
    				 L"PictureName, K7Player, DVDPlayer, "
    				 L"CDPlayer, Available) VALUES('",
                                     dlgCar->txtTagNumber->Text, L"', '",
                                     dlgCar->txtMake->Text, L"', '",
    				 dlgCar->txtModel->Text, L"', '",
    				 dlgCar->txtYear->Text, L"', '",
    				 dlgCar->txtMileage->Text, L"', '",
    				 dlgCar->cboCategory->Text, L"', '",
    				 dlgCar->txtPictureName->Text, L"', '",
    				 dlgCar->chkK7Player->Checked, L"', '",
    				 dlgCar->chkDVDPlayer->Checked, L"', '",
    				 dlgCar->chkCDPlayer->Checked, L"', '",
    				 dlgCar->chkAvailable->Checked, L"')");
    
    		 SqlClient::SqlConnection ^ conCar = 
    			gcnew SqlClient::SqlConnection(L"Data Source="
    			"(local);Database=bcr1;Integrated Security=yes");
    		 SqlClient::SqlCommand ^ cmdCar = 
    			gcnew SqlClient::SqlCommand(strNewCar, conCar);
    					 
    		 conCar->Open();
    		 cmdCar->ExecuteNonQuery();
    		 conCar->Close();
    	 }
    }
  36. Return to the form and double-click the First button
  37. Implement its event as follows:
     
    System::Void btnFirst_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 bsCars->MoveFirst();
    	 this->pctCar->Image = Image::FromFile(txtPictureName->Text);
    
    	 // This section is used to save the record
    	 // if the user had changed anything before moving on
    	 this->Validate();
    	 this->bsCars->EndEdit();
    	 this->CarsTableAdapter->Update(this->dsOrder1->Cars);
    }
  38. Return to the form and double-click the Previous button
  39. Implement its event as follows:
     
    System::Void btnPrevious_Click(System::Object^  sender,
    				 System::EventArgs^  e)
    {
    	 bsCars->MovePrevious();
    	 this->pctCar->Image = Image::FromFile(txtPictureName->Text);
    
    	 this->Validate();
    	 this->bsCars->EndEdit();
    	 this->CarsTableAdapter->Update(this->dsOrder1->Cars);
    }
  40. Return to the form and double-click the Next button
  41. Implement its event as follows:
     
    System::Void btnNext_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 bsCars->MoveNext();
    	 this->pctCar->Image = Image::FromFile(txtPictureName->Text);
    
    	 this->Validate();
    	 this->bsCars->EndEdit();
    	 this->CarsTableAdapter->Update(this->dsOrder1->Cars);
    }
  42. Return to the form and double-click the Last button
  43. Implement its event as follows:
     
    System::Void btnLast_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 bsCars->MoveLast();
    	 this->pctCar->Image = Image::FromFile(txtPictureName->Text);
    
    	 this->Validate();
    	 this->bsCars->EndEdit();
    	 this->CarsTableAdapter->Update(this->dsOrder1->Cars);
    }
  44. Return to the form and double-click the Close button
  45. Implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 Close();
    }
  46. Access the first form
  47. Add a button to it and set its properties as follows:
    Text: Cars Review
    Name: btnCarsReview
  48. Double-click the Cars Review button
  49. In the top section of the file, under the #pragma once line, type
    #include "CarsRecords.h"
  50. Scroll down and implement the event as follows:
     
    System::Void btnCarsReview_Click(System::Object^  sender,
    				 System::EventArgs^  e)
    {
    	 CarsRecords ^ frmCarsRecords = gcnew CarsRecords;
    	 frmCarsRecords->ShowDialog();
    }
  51. Execute the application to test it
  52. Using the New Car form Create a few records
  53. Close the form(s)
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next