FunctionX Practical Learning Logo

Data Entry With Windows Controls

 
 

Performing data entry to create new records using common Windows controls

Introduction

Although data entry can be performed on a data grid, Microsoft Windows provides more professional objects to create new records. Because the Microsoft .NET Framework is so huge as a library and so powerful, there are various techniques you can use to perform data entry.

Practical LearningPractical Learning: Performing Data Entry

  1. Open SQL Query Analyzer
  2. To create a new database, type the following
     
    IF EXISTS (SELECT * 
    	   FROM   master..sysdatabases 
    	   WHERE  name = N'Familia')
    	DROP DATABASE Familia
    GO
    CREATE DATABASE Familia
    GO
  3. Press F5 to execute the statement
  4. To create a new table in the above database, delete the above code and type the following:
     
    USE Familia
    GO
    IF EXISTS(SELECT name 
    	  FROM 	 sysobjects 
    	  WHERE  name = N'Genders' 
    	  AND 	 type = 'Genders')
        DROP TABLE Genders
    GO
    
    CREATE TABLE Genders (
    GenderID int NOT NULL PRIMARY KEY Identity(1, 1), 
    Gender Varchar(20) NOT NULL)
    GO
    INSERT INTO Genders (Gender) VALUES ('Unknown');
    INSERT INTO Genders (Gender) VALUES ('Female');
    INSERT INTO Genders (Gender) VALUES ('Male');
  5. Press F5 to execute the statement
  6. To create a new table in the above database, delete the above code and type the following:
     
    IF EXISTS(SELECT name 
    	  FROM 	 sysobjects 
    	  WHERE  name = N'Persons' 
    	  AND 	 type = 'Persons')
        DROP TABLE Persons
    GO
    
    CREATE TABLE Persons (
    PersonID int Primary Key NOT NULL Identity(1, 1), 
    FirstName Varchar(20) NULL,
    LastName Varchar(20) NOT NULL,
    GenderID int DEFAULT 1 REFERENCES Genders(GenderID),
    Notes Text)
    GO
    INSERT INTO Persons (FirstName, LastName, GenderID, Notes)
    VALUES('Massimo', 'Leandro', 2, 'One of the most hard working employees');
    INSERT INTO Persons (FirstName, LastName, GenderID, Notes)
    VALUES('Christina', 'Cassine', 1, 'Wonderful employees, always on time');
    INSERT INTO Persons (FirstName, LastName, GenderID, Notes)
    VALUES('Helene', 'Knowles', 1, 'Has been in the company for a while');
    INSERT INTO Persons (FirstName, LastName, GenderID, Notes)
    VALUES('Alan', 'Holmes', 3, '');
    GO
  7. Press F5 to execute the statement
  8. Open Visual C++ .NET and create a new Windows Forms Application named DataEntry2
  9. From Server Explorer, open the server that holds the above database then expand the Familia database followed by the Tables node
  10. Drag the Persons table and drop it on the form
  11. In the same way, drag Genders and drop it on the form
  12. Under the form, right-click sqlDataAdapter1 or sqlDataAdapter2 and click Generate Dataset...
  13. While the New radio button is selected, change the name of the dataset to dsPersons
  14. Select both check boxes to add both tables to the data set:
      
    Generate Dataset
  15. Click OK
  16. Using the controls on the Toolbox, design the form as follows:
     
    Control Text Name Additional Properties
    Label Person ID:    
    TextBox   txtPersonID TextAlign: Right
    ReadOnly: True
    Label First Name:    
    TextBox   txtFirstName  
    Label Last Name:    
    TextBox   txtLastName  
    Label Gender:    
    ComboBox   cboGenderID DropDownStyle: DropDownList
    Label Notes:    
    TextBox   txtNotes Multiline: True
    ScrollBars: Vertical
    Button New Record btnNewRecord  
    Button | < btnFirst  
    Button << btnPrevious  
    Button >> btnNext  
    Button > | btnLast  
    Button Close btnClose  
  17. Using the DataBindings and other fields of the Properties window, bind the controls as follows:
     
    Control Name DataBindings Other
    Type Value
    txtPersonID Text dsPersons1 - Persons.PersonID  
    txtFirstName Text dsPersons1 - Persons.FirstName  
    txtLastName Text dsPersons1 - Persons.LastName  
    cboGenderID SelectedValue dsPersons1 - Persons.GenderID DataSource: dsPersons1.Genders
    DisplayMember: Gender
    ValueMember: GenderID
    chkIsMarried Checked dsPersons1 - Persons.IsMarried  
    txtNotes Text dsPersons1 - Persons.Notes  
  18. Double-click an empty area of the form and change the Load event as follows:
     
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 this->sqlDataAdapter1->Fill(this->dsPersons1);
    			 this->sqlDataAdapter2->Fill(this->dsPersons1);
    		 }
  19. Press Ctrl + F5 to test the application
  20. After viewing the first record, close the form
  21. To allow the user to navigate through records, double-click the | <, <<, >>, and > | buttons
  22. Implement their events as follows:
     
    private: System::Void btnFirst_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	this->BindingContext->get_Item(dsPersons1, S"Persons")->Position = 0;
    }
    
    private: System::Void btnPrevious_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->BindingContext->get_Item(dsPersons1, 
                                                 S"Persons")->Position = 
    				this->BindingContext->get_Item(dsPersons1,
                                                S"Persons")->Position - 1;
    
    }
    
    private: System::Void btnNext_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->BindingContext->get_Item(dsPersons1,
    	 			      S"Persons")->Position = 
    				      this->BindingContext->get_Item(dsPersons1,
    				     "Persons")->Position + 1;
    
    }
    
    private: System::Void btnLast_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->BindingContext->get_Item(dsPersons1,
    					      S"Persons")->Position =
    		      		this->BindingContext->get_Item(dsPersons1,
    				S"Persons")->Count - 1;
    }
  23. Press Ctrl + F5 to test the application and navigate through records back and forth
  24. Close the form
  25. To allow the user to create a new record, double-click the New Record button 
  26. On the form, double the Close button
  27. Implement both events as follows:
     
    #pragma once
    
    
    namespace DataEntry2
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    
    
    	private: System::Data::SqlClient::SqlConnection *  sqlConnection1;
    
    	private: System::Data::SqlClient::SqlCommand *  sqlSelectCommand2;
    	private: System::Data::SqlClient::SqlCommand *  sqlInsertCommand2;
    	private: System::Data::SqlClient::SqlCommand *  sqlUpdateCommand2;
    	private: System::Data::SqlClient::SqlCommand *  sqlDeleteCommand2;
    	private: System::Data::SqlClient::SqlDataAdapter *  sqlDataAdapter2;
    	private: DataEntry2::dsPersons *  dsPersons1;
    	private: System::Windows::Forms::Label *  label1;
    	private: System::Windows::Forms::Label *  label2;
    
    	private: System::Windows::Forms::Label *  label3;
    	private: System::Windows::Forms::Label *  label4;
    
    	private: System::Windows::Forms::Label *  label5;
    	private: System::Windows::Forms::TextBox *  btnPersonID;
    	private: System::Windows::Forms::TextBox *  btnFirstName;
    	private: System::Windows::Forms::TextBox *  btnLastName;
    	private: System::Windows::Forms::ComboBox *  cboGenderID;
    
    	private: System::Windows::Forms::TextBox *  txtNotes;
    	private: System::Windows::Forms::Button *  btnFirst;
    	private: System::Windows::Forms::Button *  btnPrevious;
    	private: System::Windows::Forms::Button *  btnLast;
    	private: System::Windows::Forms::Button *  btnNext;
    	private: System::Windows::Forms::Button *  btnClose;
    	private: System::Windows::Forms::Button *  btnNewRecord;
    	private: System::Data::SqlClient::SqlCommand *  sqlSelectCommand1;
    	private: System::Data::SqlClient::SqlCommand *  sqlInsertCommand1;
    	private: System::Data::SqlClient::SqlCommand *  sqlUpdateCommand1;
    	private: System::Data::SqlClient::SqlCommand *  sqlDeleteCommand1;
    	private: System::Data::SqlClient::SqlDataAdapter *  sqlDataAdapter1;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{ 
    			
    			. . .
    
    		}	
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 this->sqlDataAdapter1->Fill(this->dsPersons1);
    				 this->sqlDataAdapter2->Fill(this->dsPersons1);
    			 }
    
    private: System::Void btnFirst_Click(System::Object *  sender, System::EventArgs *  e)
     {
    	 this->BindingContext->get_Item(dsPersons1, S"Persons")->Position = 0;
    	// If the user changes anything in any field and moves to another record,
    	 // update the data set (this is how it is done eveywhere, including MS SQL Server,
    	 // MS Access, Paradox, etc
    	 this->sqlDataAdapter1->Update(this->dsPersons1);
     }
    
    private: System::Void btnPrevious_Click(System::Object *  sender, System::EventArgs *  e)
     {
        this->BindingContext->get_Item(dsPersons1, 
            S"Persons")->Position = this->BindingContext->get_Item(dsPersons1,
            S"Persons")->Position - 1;
            this->sqlDataAdapter1->Update(this->dsPersons1);
     }
    
    private: System::Void btnNext_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 this->BindingContext->get_Item(dsPersons1,
    	 			      S"Persons")->Position = 
    				      this->BindingContext->get_Item(dsPersons1,
    				     S"Persons")->Position + 1;
    			 this->sqlDataAdapter1->Update(this->dsPersons1);
    		 }
    
    private: System::Void btnLast_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 this->BindingContext->get_Item(dsPersons1,
    					      S"Persons")->Position =
                                                                                          this->BindingContext->get_Item(dsPersons1,
    				S"Persons")->Count - 1;
    			 this->sqlDataAdapter1->Update(this->dsPersons1);
    		 }
    
    private: System::Void btnNewRecord_Click(System::Object *  sender, System::EventArgs *  e)
     {
    // We will use the same button to create a new record and update it
     // Find out what is the current state of the button
      if( this->btnNewRecord->Text->Equals(S"New Record") )
     {
    	 // Since the user clicked the New Record button, prepare to create a new record
    	 // We are using exception handling in case something goes wrong
         try {
    	 this->BindingContext->get_Item(this->dsPersons1, S"Persons")->EndCurrentEdit();
    	 this->BindingContext->get_Item(this->dsPersons1, S"Persons")->AddNew();
    
    	 // Since the fields are now empty, give focus to the first control that can be edited
    	 this->cboGenderID->Focus();
    
    	 // Since the user is currently creating a new record, change the caption of the button
    	 this->btnNewRecord->Text = S"Update";
         }// Did something go wrong?
         catch(System::Exception* eEdit)
         {
    	 MessageBox::Show(eEdit->Message);
        }
     }
     else // Since the user is ready with a new record, acknowledge it
    	 {
    		 // Update the whole data set
    		 this->sqlDataAdapter1->Update(this->dsPersons1);
    		 // Behave as if we were moving to the last record
    		 btnLast_Click(sender, e);
    	// Since the new record has been added, change the caption of the New Record button
    		 this->btnNewRecord->Text = S"New Record";
    		 }
    	 }
    
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    	 {
    		 Close();
    	 }
    };
    }
  28. Execute the application and click the New Record button
  29. Enter a new record and click Update
  30. Close the form
 

Home Copyright © 2004-2010 FunctionX, Inc.