FunctionX Practical Learning Logo

Data Entry Using a Data Row

 
 

Introduction

With ADO.NET, you can perform data entry using a DataGrid or a Windows control. Another option you can use is through the data set system.

Because a database is first of all a list of items with in a two-dimensional arrangement, the Microsoft .NET Framework provides the DataTable class. A DataTable object can be used to create any two-dimensional array of items as done in a flat or file-based database (a database that is not based on a database system such as SQL Server, Paradox, or MS Access but with its own ability to create and save records as files, as done in file processing). To arrange its list of items into records, the DataTable class has a Rows property. A row is simply a record. A row is of type DataRow. When you create a new record in a DataTable object, you in fact add a new DataRow object.

Because a DataSet object is made of DataTable objects, you can retrieve a table from a DataSet, add a new row to it, and then pass the table back to the DataSet to update. This is another way to perform data entry and can be valuable in a client/server multiple workstations environment.

Practical LearningPractical Learning: Performing Data Entry

 
  1. Open SQL Data Analyzer
  2. To create a new database, type the following:
     
    IF EXISTS (SELECT * 
    	   FROM   master..sysdatabases 
    	   WHERE  name = N'DaniloPizza')
    	DROP DATABASE DaniloPizza
    GO
    
    CREATE DATABASE DaniloPizza;
    GO
  3. To create a new table, delete the above code and type the following:
     
    USE DaniloPizza
    GO
    IF EXISTS (SELECT name 
    	 FROM    sysobjects 
    	 WHERE  name = N'CustomerOrder' 
    	 AND       type = 'U')
        DROP    TABLE    CustomerOrder
    GO
    
    create table CustomerOrder (
    CustomerOrderID int IDENTITY(1, 1) PRIMARY KEY NOT NULL, 
    PizzaSize smallint Default 2 NOT NULL,
    PriceSmall Decimal(4,2) Default 8.95,
    PriceMedium Decimal(4,2) Default 10.75,
    PriceLarge Decimal(4,2) Default 12.55,
    Pepperoni Bit,
    Sausage Bit,
    ExtraCheese Bit,
    Onions Bit,
    Olives Bit
    )
    GO
    INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
    VALUES (2, 8.95, 9.55, 12.55, 1, 1, 0, 0, 0);
    INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
    VALUES (1, 6.75, 8.95, 12.55, 1, 0, 1, 0, 0);
    INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
    VALUES (3, 8.95, 9.55, 11.25, 0, 1, 0, 0, 1);
    INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
    VALUES (2, 8.95, 8.55, 12.55, 0, 0, 1, 1, 0);
    GO
  4. Open Visual C# and create a new Windows Application named DataEntry3
  5. From the Data section of the Toolbox, click DataSet and click the form
  6. rver Explorer, open the server that holds the above database then expand the Danilo Pizza database followed by the Tables node
  7. Drag the CustomerOrder table and drop it on the form
  8. On the main menu, click Data -> Generate Dataset...
  9. While the New radio button is selected, change the name of the dataset to dsDaniloPizza
     
    Generate Dataset
  10. Click OK
  11. Design the form as follows: 
     
    Danilo Pizza - Customer Order
    Control Text Name Other Properties
    GroupBox Pizza Size    
    RadioButton Small rdoSmall Cursor: Hand
    TextBox 8.95 txtPriceSmall TextAlign: Right
    RadioButton Medium rdoMedium Cursor: Hand
    TextBox 10.75 txtPriceMedium TextAlign: Right
    Checked: True
    RadioButton Large rdoLarge Cursor: Hand
    TextBox 12.55 txtPriceLarge TextAlign: Right
    GroupBox Toppings    
    CheckBox Pepperoni chkPepperoni CheckAlign: MiddleRight
    Cursor: Hand
    FlatStyle: Popup
    CheckBox Sausage chkSausage CheckAlign: MiddleRight
    Cursor: Hand
    FlatStyle: Popup
    CheckBox Extra Cheese chkExtraCheese CheckAlign: MiddleRight
    Cursor: Hand
    FlatStyle: Popup
    CheckBox Onions chkOnions CheckAlign: MiddleRight
    Cursor: Hand
    FlatStyle: Popup
    CheckBox Olives chkOlives CheckAlign: MiddleRight
    Cursor: Hand
    FlatStyle: Popup
  12. 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->dsDaniloPizza1);
    		 }
  13. Double-click the Submit followed by the Close buttons
  14. Implement the events as follows:
     
    private: System::Void btnSubmit_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Int16 pizzaSize;
    			 Double priceSmall, priceMedium, priceLarge;
    			 Boolean pepperoni, sausage, extraCheese, onions, olives;
    
    			 // Find out what radio button was clicked
    			 if( this->rdoSmall->Checked == true )
    				 pizzaSize = 1;
    			 else if( this->rdoMedium->Checked == true )
    				 pizzaSize = 2;
    			 else if( this->rdoLarge->Checked == true )
    				 pizzaSize = 3;
    
    			 // Retrieve the values of pizza prices
    			 priceSmall  = this->txtPriceSmall->Text->ToDouble(0);
    			 priceMedium = this->txtPriceMedium->Text->ToDouble(0);
    			 priceLarge  = this->txtPriceLarge->Text->ToDouble(0);
    
    			 // Retrieve the state of each check box
    			 pepperoni   = this->chkPepperoni->Checked;
    			 sausage     = this->chkSausage->Checked;
    			 extraCheese = this->chkExtraCheese->Checked;
    			 onions      = this->chkOnions->Checked;
    			 olives		 = this->chkOlives->Checked;
    
    			 // Now that a new record has been created, prepare to save it
    		 // First create a DataTable variable of the table used in the current DataSet
    		DataTable *tblCustOrder = this->dsDaniloPizza1->Tables->Item[S"CustomerOrder"];
    			 // Create a new row from the current table
    			 DataRow   *rowRecord    = tblCustOrder->NewRow();
    			 
    			 // Fill the new row with values entered on the form
    			 rowRecord->Item[S"PizzaSize"]   = pizzaSize.ToString();
    			 rowRecord->Item[S"PriceSmall"]  = priceSmall.ToString();
    			 rowRecord->Item[S"PriceMedium"] = priceMedium.ToString();
    			 rowRecord->Item[S"PriceLarge"]  = priceLarge.ToString();
    			 rowRecord->Item[S"Pepperoni"]   = pepperoni.ToString();
    			 rowRecord->Item[S"Sausage"]     = sausage.ToString();
    			 rowRecord->Item[S"ExtraCheese"] = extraCheese.ToString();
    			 rowRecord->Item[S"Onions"]      = onions.ToString();
    			 rowRecord->Item[S"Olives"]      = olives.ToString();
    
    			 // Pass the new record to the table
    			 tblCustOrder->Rows->Add(rowRecord);
    
    			 // Update the DataSet with information in the new record
    			 this->sqlDataAdapter1->Update(this->dsDaniloPizza1);
    
    			 // In case the user wants to create a new record, reset the controls
    			 this->rdoMedium->Checked        = true;
    			 this->txtPriceSmall->Text            = S"8.95";
    			 this->txtPriceMedium->Text         = S"10.75";
    			 this->txtPriceLarge->Text            = S"12.55";
    			 this->chkPepperoni->Checked     = false;
    			 this->chkSausage->Checked       = false;
    			 this->chkExtraCheese->Checked = false;
    			 this->chkOnions->Checked          = false;
    			 this->chkOnions->Checked          = false;
    		 }
    
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Close();
    		 }
  15. Return to SQL Data Analyzer. Open a new Blank Query Window and type:
     
    SELECT * FROM CustomerOrder
    GO;
  16. Press F5 to execute
  17. Return to Visual C++ .NET and press Ctrl + F5 to test the application
  18. Create a new record by entering new information then click Submit
  19. Return to SQL Data Analyzer and press F5 to execute the statement again

 

 


Home Copyright © 2004-2010 FunctionX, Inc.