Home

Lists-Based Applications: The Music Store

 

Introduction

The DataSet of the .NET Framework is a very impressive class. It allows you to create and manage any type of list, making it a central point for a list-based application, although it is highly used in formal database applications such as those intended for Microsoft SQL Server or Microsoft Access.

To support the creation and management of any type of list, the DataSet class has complete built-in mechanisms for creating tables, their associated columns, and to perform data entry. To demonstrate these functionalities of the DataSet, we are going to create a list-based application that can serve as another introduction to databases. The application simulates a music instrument store that processes orders for customers.

When a customer places an order in the store to purchase a product, the employee can select items by categories, specify the quantity, calculate the total order, and save it.

Practical Learning Practical Learning: Creating the Application

  1. Start Microsoft Visual Studio .NET and create a new Windows Application named MusicStore2
  2. To add a new form, on the main menu, click Project -> Windows Form...
  3. Set the Name to DataCenter and press Enter
  4. To add a new form, on the main menu, click Project -> Windows Form...
  5. Set the Name to Categories and press Enter
  6. Design the form as follows:
     
    Control Name Text Other Properties
    DataGrid     Auto Format: Professional 3
    Label   New Category:  
    TextBox txtNewCategory    
    Button btnAdd Add  
    Button btnClose Close  
    Form     AcceptButton: btnAdd
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    ShowInTaskbar: False
    StartPosition: CenterScreen
  7. To add a new form, on the main menu, click Project -> Windows Form...
  8. Set the Name to ItemTypes and press Enter
  9. Design the form as follows:
     
    Control Name Text Other Properties
    DataGrid     Auto Format: Professional 3
    Label   New Item Type:  
    TextBox txtNewItemType    
    Button btnAdd Add  
    Button btnClose Close  
    Form     AcceptButton: btnAdd
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    ShowInTaskbar: False
    StartPosition: CenterScreen
  10. To add a new form, on the main menu, click Project -> Windows Form...
  11. Set the Name to NewStoreItem and press Enter
  12. Design the form as follows:
     
    Control Name Text Other Properties
    Label   Category:  
    ComboBox cboCategories   DropDownStyle: DropDownList
    Sorted: True
    Button btnNewCategory New  
    Label   Item Type:  
    ComboBox cboItemTypes   DropDownStyle: DropDownList
    Sorted: True
    Button btnNewType New  
    Label   Item Name:  
    TextBox txtItemName    
    Label   Unit Price:  
    TextBox txtUnitPrice 0.00 AlignText: Right
    Label   Item #:  
    TextBox txtItemNumber    
    Button btnCreate Create  
    Button btnClose Close  
    Form     AcceptButton: btnCreate
    CancelButton: btnClose
    MaximizeBox: False
    StartPosition: CenterScreen
  13. Double-click an unoccupied area of the form to access its Load event and implement its as follows:
     
    private void NewStoreItem_Load(object sender, System.EventArgs e)
    {
    	// We will generate a random number for the store item
    	DateTime tmeNow = DateTime.Now;
    	Random rndNumber = new Random(tmeNow.Millisecond);
    	string strNumber = rndNumber.Next(100000, 999999).ToString();
    
    	// Display the new number in the Part # text box
    	this.txtItemNumber.Text = strNumber;
    
    	// Disable the OK button to indicate that the item is not ready
    	this.btnCreate.Enabled = false;
    }
  14. Return to the NewStoreItem form
  15. Double-click the top New button on the right side of the Category combo box and implement its event as follows:
     
    private void btnNewCategory_Click(object sender, System.EventArgs e)
    {
    	Categories frmCat = new Categories();
    
    	 frmCat.ShowDialog();
    }
  16. Return to the NewStoreItem form and double-click the lower New button
  17. Implement the Click event as follows:
     
    private void btnNewType_Click(object sender, System.EventArgs e)
    {
    	ItemTypes frmTypes = new ItemTypes();
    
    	frmTypes.ShowDialog();
    }
  18. To add a new form, on the main menu, click Project -> Add Windows Form...
  19. Set the Name to PurchaseOrder and press Enter
  20. Design the form as follows:
     
    Control Name Text Other Properties
    GroupBox   Selection of Sale Item  
    Label   Category  
    Label   Types  
    Label   Available Items  
    Button btnNewItem New Item  
    ListBox lbxCategories    
    ListBox lbxItemTypes    
    ListBox lbxAvailableItems    
    GroupBox   Items Sold  
    Label   Item #  
    Label   Item Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtItemNumber1    
    TextBox txtItemName1    
    TextBox txtUnitPrice1 0.00 AlignText: Right
    TextBox txtQuantity1 0 AlignText: Right
    TextBox txtSubTotal1 0.00 AlignText: Right
    TextBox txtItemNumber2    
    TextBox txtItemName2    
    TextBox txtUnitPrice2 0.00 AlignText: Right
    TextBox txtQuantity2 0 AlignText: Right
    TextBox txtSubTotal2 0.00 AlignText: Right
    TextBox txtItemNumber3    
    TextBox txtItemName3    
    TextBox txtUnitPrice3 0.00 AlignText: Right
    TextBox txtQuantity3 0 AlignText: Right
    TextBox txtSubTotal3 0.00 AlignText: Right
    TextBox txtItemNumber4    
    TextBox txtItemName4    
    TextBox txtUnitPrice4 0.00 AlignText: Right
    TextBox txtQuantity4 0 AlignText: Right
    TextBox txtSubTotal4 0.00 AlignText: Right
    TextBox txtItemNumber5    
    TextBox txtItemName5    
    TextBox txtUnitPrice5 0.00 AlignText: Right
    TextBox txtQuantity5 0 AlignText: Right
    TextBox txtSubTotal5 0.00 AlignText: Right
    TextBox txtItemNumber6    
    TextBox txtItemName6    
    TextBox txtUnitPrice6 0.00 AlignText: Right
    TextBox txtQuantity6 0 AlignText: Right
    TextBox txtSubTotal6 0.00 AlignText: Right
    GroupBox   Order Processing  
    Button btnCalculate Calculate Order  
    Button btnSave Add to Today's Orders  
    Button btnClose Close  
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 AlignText: Right
    Form     CancelButton: btnClose
    MaximizeBox: False
    StartPosition: CenterScreen
  21. Double-click the NewItem button
  22. Implement its Click event as follows:
     
    private void btnNewItem_Click(object sender, System.EventArgs e)
    {
    	NewStoreItem frm = new NewStoreItem();
    	frm.ShowDialog();
    }
  23. Double-click the Calculate Order button and implement its Click event as follows:
     
    private void btnCalculate_Click(object sender, System.EventArgs e)
    {
    	decimal item1UnitPrice= 0.00M, item2UnitPrice= 0.00M, item3UnitPrice= 0.00M,
    		item4UnitPrice= 0.00M, item5UnitPrice= 0.00M, item6UnitPrice= 0.00M;
    	int quantity1 = 0, quantity2 = 0, quantity3 = 0,
    		quantity4 = 0, quantity5 = 0, quantity6 = 0;
    	decimal item1SubTotal= 0.00M, item2SubTotal= 0.00M, item3SubTotal= 0.00M,
    		item4SubTotal= 0.00M, item5SubTotal= 0.00M, item6SubTotal= 0.00M;
    	decimal totalOrder;
    
    	try 
    	{
    		item1UnitPrice = decimal.Parse(this.txtUnitPrice1.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice1.Text = "0.00";
    		this.txtUnitPrice1.Focus();
    	}
    
    	try 	
    	{
    		quantity1 = int.Parse(this.txtQuantity1.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity1.Text = "0";
    		this.txtQuantity1.Focus();
    	}
    
    	try 
    	{
    		item2UnitPrice = decimal.Parse(this.txtUnitPrice2.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice2.Text = "0.00";
    		this.txtUnitPrice2.Focus();
    	}
    
    	try 						
    	{
    		quantity2 = int.Parse(this.txtQuantity2.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity2.Text = "0";
    		this.txtQuantity2.Focus();
    	}
    
    	try 
    	{
    		item3UnitPrice = decimal.Parse(this.txtUnitPrice3.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice3.Text = "0.00";
    		this.txtUnitPrice3.Focus();
    	}
    
    	try 
    	{
    		quantity3 = int.Parse(this.txtQuantity3.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity3.Text = "0";
    		this.txtQuantity3.Focus();
    	}
    
    	try 
    	{
    		item4UnitPrice = decimal.Parse(this.txtUnitPrice4.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice4.Text = "0.00";
    		this.txtUnitPrice4.Focus();
    	}
    
    	try 
    	{
    		quantity4 = int.Parse(this.txtQuantity4.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity4.Text = "0";
    		this.txtQuantity4.Focus();
    	}
    
    	try 
    	{
    		item5UnitPrice = decimal.Parse(this.txtUnitPrice5.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice5.Text = "0.00";
    		this.txtUnitPrice5.Focus();
    	}
    
    	try 
    	{
    		quantity5 = int.Parse(this.txtQuantity5.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity5.Text = "0";
    		this.txtQuantity5.Focus();
    	}
    
    	try 
    	{
    		item6UnitPrice = decimal.Parse(this.txtUnitPrice6.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Unit Price");
    		this.txtUnitPrice6.Text = "0.00";
    		this.txtUnitPrice6.Focus();
    	}
    
    	try 
    	{
    		quantity6 = int.Parse(this.txtQuantity6.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Quantity");
    		this.txtQuantity6.Text = "0";
    		this.txtQuantity6.Focus();
    	}
    
    	item1SubTotal = item1UnitPrice * quantity1;
    	item2SubTotal = item2UnitPrice * quantity2;
    	item3SubTotal = item3UnitPrice * quantity3;
    	item4SubTotal = item4UnitPrice * quantity4;
    	item5SubTotal = item5UnitPrice * quantity5;
    	item6SubTotal = item6UnitPrice * quantity6;
    
    	totalOrder    = item1SubTotal + item2SubTotal + item3SubTotal +
    		item4SubTotal + item5SubTotal + item6SubTotal;
    
    	this.txtSubTotal1.Text = item1SubTotal.ToString("F");
    	this.txtSubTotal2.Text = item2SubTotal.ToString("F");
    	this.txtSubTotal3.Text = item3SubTotal.ToString("F");
    	this.txtSubTotal4.Text = item4SubTotal.ToString("F");
    	this.txtSubTotal5.Text = item5SubTotal.ToString("F");
    	this.txtSubTotal6.Text = item6SubTotal.ToString("F");
    	this.txtTotalOrder.Text = totalOrder.ToString("F");
    }
  24. Display the first form (Form1.cs [Design]) and design it as follows:
     
    Control Name Text
    Button btnNewItem New Store Item
    Button btnNewOrder New Purchase Order
    Button btnClose Close
  25. Double-click the New Store Item button and implement its event as follows:
     
    private void btnNewItem_Click(object sender, System.EventArgs e)
    {
    	NewStoreItem frm = new NewStoreItem();
    	frm.ShowDialog();
    }
  26. Return to the form. Double-click the New Purchase Order and implement its event as follows:
     
    private void btnNewOrder_Click(object sender, System.EventArgs e)
    {
    	PurchaseOrder frm = new PurchaseOrder();
    	frm.ShowDialog();
    }
  27. Return to the main form. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  28. Save all
 

Application Data Entry

To perform data entry, we will apply the concepts reviewed for data records.

Practical Learning Practical Learning: Introducing Data Entry

  1. Display the DataCenter form (DataCenter.cs [Design])
  2. In the Toolbox, click the Data button.
    To create a DataSet, click DataSet and click the DataModule form
     
  3. In the Add Dataset dialog box, click the Untyped Dataset radio button and click OK
  4. While the DataSet control is still selected, in the Properties window, click (Name) and type dsMusicStore
  5. Still in the Properties window, set the DataSetName to MusicStore
  6. Set its Modifiers property to Public so it can be easily accessed from other objects
  7. Click the Tables field to reveal its ellipsis button ellipsis and click the ellipsis button ellipsis
  8. In the Tables Collection Editor, click the Add button
  9. Click (Name) and type tblCategories
  10. Click TableName and type Categories
  11. Set its Modifiers property to Public
  12. Click the Columns field to reveal its ellipsis button ellipsis and click its ellipsis button ellipsis
  13. In the Columns Collection Editor, click the Add button
  14. Change the (Name) to colCategory and change the ColumnName field to Category
  15. Set its Modifiers property to Public
  16. Click Close
  17. In the same way, create a table as
    (Name): tblItemTypes
    TableName: ItemTypes
    Modifiers: Public
  18. Add a column with the following properties:
    (Name): colItemType
    ColumnName: ItemType
    Modifiers: Public

     
  19. In the same way, create a table as
    (Name): tblAvailableItems
    TableName: AvailableItems
    Modifiers: Public
  20. Add the following columns to it
     
    (Name) ColumnName Modifiers
    colItemNumber ItemNumber Public
    colItemCategory Category Public
    colTypeOfItem ItemType Public
    colItemName ItemName Public
    colUnitPrice UnitPrice Public
  21. Click Close twice
  22. Display the Categories form and double-click its Add button
  23. In the top section of the file, type
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Data;
  24. Implement the event as follows:
     
    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    	// Get a reference to the Data Center because 
    	// that's where the DataSet object resides
    	DataCenter frmData = new DataCenter();
    	// This is the XML file that will holds the Categories
    	string strFilename = "Categories.xml";
    
    	// If the file exists already, open it
    	if( File.Exists(strFilename) )
    		frmData.dsMusicStore.ReadXml(strFilename);
    
    	// Create a new record for the Categories table
    	DataRow rowNewCategory = frmData.dsMusicStore.Tables["Categories"].NewRow();
    	// Specify only the Category column since the CategoryID is auto-incrementing
    	rowNewCategory[0] = this.txtNewCategory.Text;
    	// Add the new record to the Categories table
    	frmData.dsMusicStore.Tables["Categories"].Rows.Add(rowNewCategory);
    
    	// Update the XML file
    	frmData.dsMusicStore.WriteXml(strFilename);
    
    	// Display the current records of the Categories table so
    	// the user would know what categories are already in the table
    	this.dataGrid1.DataSource = frmData.dsMusicStore;
    	this.dataGrid1.DataMember = "Categories";
    
    	// Reset the text box in case the user wants to add another category
    	this.txtNewCategory.Text = "";
    	this.txtNewCategory.Focus();
    }
  25. Display the ItemTypes form and double-click its Add button
  26. In the top section of the file, type
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Data;
  27. Implement the event as follows:
     
    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    	DataCenter frmData = new DataCenter();
    	string strFilename = "ItemTypes.xml";
    
    	if( File.Exists(strFilename) )
    		frmData.dsMusicStore.ReadXml(strFilename);
    
    	DataRow rowNewType = frmData.dsMusicStore.Tables["ItemTypes"].NewRow();
    	rowNewType[frmData.colItemType] = this.txtNewItemType.Text;
    	frmData.dsMusicStore.Tables["ItemTypes"].Rows.Add(rowNewType);
    
    	frmData.dsMusicStore.WriteXml(strFilename);
    
    	this.dataGrid1.DataSource = frmData.dsMusicStore;
    	this.dataGrid1.DataMember = "ItemTypes";
    
    	this.txtNewItemType.Text = "";
    	this.txtNewItemType.Focus();
    }
  28. Save all

Saving the Items and the Orders

Based on the close relationship between the DataSet class and XML, we will save our records as XML.

Practical Learning Practical Learning: Opening an XML File

  1. Display the New Store Item form.
    Double-click an unoccupied area of its body
  2. In the top section of the file, type
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Data;
  3. Change its Load event as follows:
     
    private void NewStoreItem_Load(object sender, System.EventArgs e)
    {
    	DataCenter frmData = new DataCenter();
    	 XmlTextReader rdrMusicStore = null;
    				 
    	 try {
    		 string strFilename = "Categories.xml";
    
    		 if( File.Exists(strFilename) )
    		 {
    			 rdrMusicStore = new XmlTextReader(strFilename);
    
    			 // Scan the XML file
    			 while (rdrMusicStore.Read())
    			 {
    				 // every time you find an element, find out what type it is
    				 // If you find a category, put it in the Categories list box
    			 if( (XmlNodeType.Element != 0) && (rdrMusicStore.Name == "Category") )
    			 {
    		 this.cboCategories.Items.Add(rdrMusicStore.ReadElementString("Category"));
    			 }       
                    		 }
    	                }
    
                    	 strFilename = "ItemTypes.xml";
    
                    	 if( File.Exists(strFilename) )
                    	 {
    		 rdrMusicStore = new XmlTextReader(strFilename);
    
    		 // Scan the XML file
    		 while (rdrMusicStore.Read())
    		 {
    			 // every time you find an element, find out what type it is
    			 // If you find an ItemType, put it in the Item Types list box
    			 if( (XmlNodeType.Element != 0) && (rdrMusicStore.Name == "ItemType") )
    			 {
    		 this.cboItemTypes.Items.Add(rdrMusicStore.ReadElementString("ItemType"));
    			 }       
    		 }
                    	 }
                    }
    	 catch(XmlException)
    	 {
    		 MessageBox.Show("Invalid XML file");
    	 }
    	 finally
    	 {
    		 if( rdrMusicStore != null )
    			 rdrMusicStore.Close();
    	 }
    
    	 // We will generate a random number for the store item
    	 DateTime tmeNow = DateTime.Now;
    	 Random rndNumber = new Random(tmeNow.Millisecond);
    	 string strNumber = rndNumber.Next(100000, 999999).ToString();
    
    	 // Display the new number in the Part # text box
    	 this.txtItemNumber.Text = strNumber;
    
    	 // Disable the Create button to indicate that the item is not ready
    	 this.btnCreate.Enabled = false;
    }		
  4. Return to the New Store Item form. Double-click the top New button and change its Click event as follows:
     
    private void btnNewCategory_Click(object sender, System.EventArgs e)
    {
    	Categories frmCat = new Categories();
    
    	 frmCat.ShowDialog();
    			 
    	 XmlTextReader rdrMusicStore = null;
    				 
    	 try {
    		 string strFilename = "Categories.xml";
    
    		 if( File.Exists(strFilename) )
    		 {
    			 this.cboCategories.Items.Clear();
    
    			 rdrMusicStore = new XmlTextReader(strFilename);
    
    			 // Scan the XML file
    			 while (rdrMusicStore.Read())
    			 {
    		 // every time you find an element, find out what type it is
    		 // If you find a category, put it in the Categories list box
    	 if( (XmlNodeType.Element != 0) && (rdrMusicStore.Name == "Category") )
    				 {
    		 string strNew = rdrMusicStore.ReadElementString("Category");
    			 if( !this.cboCategories.Items.Contains(strNew) )
    				 this.cboCategories.Items.Add(strNew);
    				 }       
    			 }
    		 }
    	 }
    	 catch(XmlException)
    	 {
    		 MessageBox.Show("Invalid XML file");
    	 }
    	 finally
    	 {
    		 if( rdrMusicStore != null )
    			 rdrMusicStore.Close();
    	 }
    }
  5. Return to the New Store Item form. Double-click the other New button and change its Click event as follows:
     
    private void btnNewType_Click(object sender, System.EventArgs e)
    {
    	ItemTypes frmTypes = new ItemTypes();
    
    	 frmTypes.ShowDialog();
    			 				 
    	 XmlTextReader rdrMusicStore = null;
    				 
    	 try {
    		 string strFilename = "ItemTypes.xml";
    
    		 if( File.Exists(strFilename) )
    		 {
    			 rdrMusicStore = new XmlTextReader(strFilename);
    
    			 while (rdrMusicStore.Read())
    			 {
    	 if( (XmlNodeType.Element != 0) && (rdrMusicStore.Name == "ItemType") )
    				 {
    	 string strNewType = rdrMusicStore.ReadElementString("ItemType");
    	 if( !this.cboItemTypes.Items.Contains(strNewType) )
    		 this.cboItemTypes.Items.Add(strNewType);
    				 }       
    			 }
    		 }
    	 }
    	 catch(XmlException)
    	 {
    		 MessageBox.Show("Invalid XML file");
    	 }
    	 finally
    	 {
    		 if( rdrMusicStore != null )
    			 rdrMusicStore.Close();
    	 }
    }
  6. Return to the New Store Item form. Double-click the Item Name text box and implement its TextChanged event as follows:
     
    private void txtItemName_TextChanged(object sender, System.EventArgs e)
    {
    	if( this.txtItemName.Text == "" )
    		this.btnCreate.Enabled = false;
    	else
    		this.btnCreate.Enabled = true;
    }
  7. Return to the New Store Item form. Double-click the Unit Price text box and implement its TextChanged event as follows:
     
    private void txtUnitPrice_TextChanged(object sender, System.EventArgs e)
    {
    	if( this.txtUnitPrice.Text == "" )
    		this.btnCreate.Enabled = false;
    	else
    		this.btnCreate.Enabled = true;
    }
  8. Return to the New Store Item form. Double-click the Item Number text box and implement its TextChanged event as follows:
     
    private void txtItemNumber_TextChanged(object sender, System.EventArgs e)
    {
    	if( this.txtItemNumber.Text == "" )
    		this.btnCreate.Enabled = false;
    	else
    		this.btnCreate.Enabled = true;
    }
  9. Return to the New Store Item form. Double-click its Create button and implement the event as follows:
     
    private void btnCreate_Click(object sender, System.EventArgs e)
    {
    	// Get a reference to the Data Center because 
    	// that's where the DataSet object resides
    	DataCenter frmData = new DataCenter();
    
    	// This is the XML file that will holds the general inventory
    	// of the current items in the store
    	string strAvailableItems = "Inventory.xml";
    
    	// If the file exists already, open it
    	if( File.Exists(strAvailableItems) )
    		frmData.dsMusicStore.ReadXml(strAvailableItems);
    
    	// Create a new record for the AvailableItems table
    DataRow rowNewItem = frmData.dsMusicStore.Tables["AvailableItems"].NewRow();
    
    	rowNewItem["ItemNumber"] = this.txtItemNumber.Text;
    	rowNewItem["Category"] = this.cboCategories.Text;
    	rowNewItem["ItemType"] = this.cboItemTypes.Text;
    	rowNewItem["ItemName"]   = this.txtItemName.Text;
    	rowNewItem["UnitPrice"]  = this.txtUnitPrice.Text;
    	// Add the new record to the AvailableItems table
    	frmData.dsMusicStore.Tables["AvailableItem"].Rows.Add(rowNewItem);
    
    	// Update the XML file
    	frmData.dsMusicStore.WriteXml(strAvailableItems);
    
    	// Reset the controls in case the user wants to add another record
    	this.cboCategories.SelectedIndex = -1;
    	this.cboItemTypes.SelectedIndex  = -1;
    	this.txtItemName.Text = "";
    	this.txtUnitPrice.Text = "0.00";
    			 
    	// We will generate a random number for the store item
    	DateTime tmeNow = DateTime.Now;
    	Random rndNumber = new Random(tmeNow.Millisecond);
    	string strNumber = rndNumber.Next(100000, 999999).ToString();
    
    	// Display the new number in the Part # text box
    	this.txtItemNumber.Text = strNumber;
    
    	// Disable the OK button to indicate that the item is not ready
    	this.btnCreate.Enabled = false;
    }
  10. Execute the application
  11. Click the New Store Item button
  12. Click the top New button to display the Categories form and create a few categories as follows:
     
    Category
    Electric Guitar
    Acoustic Guitar
    Bass
    Keyboard
    Stands
    Cables
    Cases
    DJ Equipment
  13. Close the Categories form and open the Item Types form
  14. Create a few item types as follows:
     
    Item Type
    4-String
    5-String
    6-String
    12-String
    Solid Body
    Hollow Body
    Left-Handed
    Guitar Stand
    DJ CD Players
  15. Close the Item Types form
  16. In the New Store Item form, create the following items (let the computer generate item numbers):
     
    Category Item Type Item Name Unit Price
    Electric Guitar Solid Body Gibson Les Paul Standard Electric Guitar 1950.00
    Bass 6-String Ibanez SR506 6-string Electric Bass 595.50
    Electric Guitar Hollow Body Oscar Schmidt OE40 Hollow Body Electric Guitar 205.95
    Acoustic Guitar Left-Handed Yamaha Left-Handed FG413SL Acoustic Guitar 295.95
    Cables Instrument Cable Monster Cable S-100 Straight Instrument Cable 12.95
    Bass Combo Amps Behringer BX1200 Ultrabass Amplifier (120 Watts, 2-Channel) 150.00
    Keyboard Synthesizers Korg Triton Le 61-Key Workstation Synth 895.95
    Stands Guitar Stand string Swing Metal Guitar Wall Hanger 9.95
    Electric Guitar Solid Body Epiphone LP-100 Electric Guitar 275.95
    Keyboard Digital Piano Yamaha YDP223 88-Key Graded Hammer Piano With Bench 1490.00
    Cables Guitar Cable Spectraflex Guitar Cable 42.25
    Electric Guitar Solid Body Gibson Les Paul Classic Electric Guitar 1625.95
    Stands Guitar Stand Locking Tubular Guitar Stand 4.95
    Electric Guitar Solid Body ESP LTD Viper 400 Electric Guitar 585.50
    Acoustic Guitar 12-String Washburn J28S12DL Cumberland 12-string Guitar 650.75
    Electric Guitar Hollow Body Ibanez Artcore AF75 Electric Guitar 315.95
    Keyboard Synthesizers Korg Triton Extreme 61-Key Synth Workstation 1895.00
    Electric Guitar Solid Body Epiphone Les Paul Standard 525.95
    Stands Guitar Stand Guitar Folding Stand 12.95
    Electric Guitar Hollow Body Gibson ES-335 Reissue 1850.00
    Acoustic Guitar 12-String Martin DM12 850.00
    Stands Guitar Stand Metal Guitar Wall Hanger 9.95
    Acoustic Guitar Left Handed Yamaha FG413SL 295.95
    Electric Guitar Solid Body Gibson Faded SG Special 625.95
    Bass 4-String Ibanez SR500 525.95
    Electric Guitar Hollow Body Oscar Schmidt OE40 225.95
    Electric Guitar Left Handed Schecher C-1 450.95
    Cases Cases Les Paul Hardshell Case 39.95
    Cables Instrument Cable Hosa Dual Instrument Cable 7.25
  17. Close the forms and return to your programming environment
  18. Display the Purchase Order form and double-click an unoccupied area of its body away from (outside of) any group box
  19. In the top section of the file, type
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Data;
  20. Implement the Load event as follows:
     
    private void PurchaseOrder_Load(object sender, System.EventArgs e)
    {
    	// Get a reference to the object that holds the DataSet
    	DataCenter frmData = new DataCenter();
    	// Identify the file that holds the categories
    	string strCategories = "Categories.xml";
    
    	// If that file exists, then open it
    	if( File.Exists(strCategories) )
    		frmData.dsMusicStore.ReadXml(strCategories);
    
    	// Just in case, empty the Categories list box
    	this.lbxCategories.Items.Clear();
    
    	DataRow row = null;
    	// Scan the whole file to locate each category and retrieve it
    	for(int i = 0; i < frmData.tblCategories.Rows.Count; i++)
    	{
    		row = frmData.tblCategories.Rows[i];
    		this.lbxCategories.Items.Add((string)(row["Category"]));
    	}
    }
  21. Return to the Purchase Order form and double-click the Category list box
  22. Implement its SelectedIndexChanged event as follows:
     
    private void lbxCategories_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	// Get a reference to the object that holds the DataSet
    	DataCenter frmData = new DataCenter();
    	// Identify the file that holds the items
    	string strAvailableItems = "Inventory.xml";
    	// Identify the category that the user selected
    	string strSelectedCategory = this.lbxCategories.Text;
    
    	// If that file exists, then open it
    	if( File.Exists(strAvailableItems) )
    		frmData.dsMusicStore.ReadXml(strAvailableItems);
    
    	// Empty the Item Types list box
    	this.lbxItemTypes.Items.Clear();
    	// Also empty the Available Items list box
    	this.lbxAvailableItems.Items.Clear();
    
    	DataRow row = null;
    	// Scan the whole table to locate each record
    	for(int i = 0; i < frmData.tblAvailableItems.Rows.Count; i++)
    	{
    		// Get a reference to the current record
    		row = frmData.tblAvailableItems.Rows[i];
    		// Get the name of the category of the current record
    		string strCurrentCategory = (string)(row["Category"]);
    
    		// If the current category matches the one the user selected...
    		if( strCurrentCategory == strSelectedCategory )
    		{
    			// ... then get the corresponding item type
    			string strType     = (string)(row["ItemType"]);
    					
    			// Find out if the Item Types list box already contains the item type
    			// If it doesn't, then put it in the Item Types list box
    			if( !this.lbxItemTypes.Items.Contains(strType) )
    				this.lbxItemTypes.Items.Add(strType);
    		}
    	}
    }
  23. Return to the Purchase Order form and double-click the Item Types list box
  24. Implement its SelectedIndexChanged event as follows:
     
    private void lbxItemTypes_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	// Get a reference to the object that holds the DataSet
    	DataCenter frmData = new DataCenter();
    	// Identify the file that holds the items
    	string strAvailableItems = "Inventory.xml";
    	// Identify the category that the user selected
    	string strSelectedCategory = this.lbxCategories.Text;
    	string strSelectedType     = this.lbxItemTypes.Text;
    
    	// If file exists, then open it
    	if( File.Exists(strAvailableItems) )
    		frmData.dsMusicStore.ReadXml(strAvailableItems);
    
    	// Empty the Available Items list box
    	this.lbxAvailableItems.Items.Clear();
    
    	DataRow row = null;
    	// Scan the whole table to locate each record
    	for(int i = 0; i < frmData.tblAvailableItems.Rows.Count; i++)
    	{
    		// Get a reference to the current record
    		row = frmData.tblAvailableItems.Rows[i];
    		// Get the name of the category of the current record
    		string strCurrentCategory = (string)(row["Category"]);
    		// Get the item type of the current record
    		string strCurrentType = (string)(row["ItemType"]);
    
    		// If the current category matches the one the user selected
    		// and the current item type matches the type the user selected ...
    		if( (strCurrentCategory == strSelectedCategory) &&
    		    (strCurrentType == strSelectedType) )
    		{
    		         // ... then get the corresponding item name
    		         this.lbxAvailableItems.Items.Add((string)(row["ItemName"]));
    		}
    	}
    }
  25. Return to the Purchase Order form and click the Available Items list box
  26. In the Properties window, click the Events button and double-click the DoubleClick field
  27. Implement its DoubleClick event as follows:
     
    private void lbxAvailableItems_DoubleClick(object sender, System.EventArgs e)
    {
    	// Get a reference to the object that holds the DataSet
    	DataCenter frmData = new DataCenter();
    	// Identify the file that holds the items
    	string strAvailableItems = "Inventory.xml";
    	// Identify the item that the user selected
    	string strSelectedCategory = this.lbxCategories.Text;
    	string strSelectedType     = this.lbxItemTypes.Text;
    	string strSelectedName     = this.lbxAvailableItems.Text;
    
    	// If file exists, then open it
    	if( File.Exists(strAvailableItems) )
    		frmData.dsMusicStore.ReadXml(strAvailableItems);
    
    	DataRow row = null;
    	// Scan the whole table to locate each record
    	for(int i = 0; i < frmData.tblAvailableItems.Rows.Count; i++)
    	{
    		// Get a reference to the current record
    		row = frmData.tblAvailableItems.Rows[i];
    		// Get the name of the category of the current record
    		string strCurrentCategory = (string)(row["Category"]);
    		// Get the item type of the current record
    		string strCurrentType = (string)(row["ItemType"]);
    		// Get the name of the current item
    		string strCurrentName = (string)(row["ItemName"]);
    		// Get the item number of the current item
    		string strCurrentNumber = (string)(row["ItemNumber"]);
    		// Get the unit price of the current item
    		string strCurrentPrice = (string)(row["UnitPrice"]);
    
    		// If the current parts match the ones the user selected ...
    		if( (strCurrentCategory == strSelectedCategory) &&
    		    (strCurrentType == strSelectedType) &&
    		    (strCurrentName == strSelectedName) )
    		{
    			// ... then consider the corresponding item
    			if( this.txtItemNumber1.Text == "" )
    			{
    				this.txtItemNumber1.Text = strCurrentNumber;
    				this.txtItemName1.Text   = strCurrentName;
    				this.txtUnitPrice1.Text  = strCurrentPrice;
    				this.txtQuantity1.Text   = "1";
    				this.txtSubTotal1.Text   = strCurrentPrice;
    			}
    			else if( this.txtItemNumber2.Text== "" )
    			{
    				this.txtItemNumber2.Text = strCurrentNumber;
    				this.txtItemName2.Text   = strCurrentName;
    				this.txtUnitPrice2.Text  = strCurrentPrice;
    				this.txtQuantity2.Text   = "1";
    				this.txtSubTotal2.Text   = strCurrentPrice;
    			}
    			else if( this.txtItemNumber3.Text== "" )
    			{
    				this.txtItemNumber3.Text = strCurrentNumber;
    				this.txtItemName3.Text   = strCurrentName;
    				this.txtUnitPrice3.Text  = strCurrentPrice;
    				this.txtQuantity3.Text   = "1";
    				this.txtSubTotal3.Text   = strCurrentPrice;
    			}
    			else if( this.txtItemNumber4.Text== "" )
    			{
    				this.txtItemNumber4.Text = strCurrentNumber;
    				this.txtItemName4.Text   = strCurrentName;
    				this.txtUnitPrice4.Text  = strCurrentPrice;
    				this.txtQuantity4.Text   = "1";
    				this.txtSubTotal4.Text   = strCurrentPrice;
    			}
    			else if( this.txtItemNumber5.Text== "" )
    			{
    				this.txtItemNumber5.Text = strCurrentNumber;
    				this.txtItemName5.Text   = strCurrentName;
    				this.txtUnitPrice5.Text  = strCurrentPrice;
    				this.txtQuantity5.Text   = "1";
    				this.txtSubTotal5.Text   = strCurrentPrice;
    			}
    			else // if( this.txtItemNumber6.Text== "" )
    			{
    				this.txtItemNumber6.Text = strCurrentNumber;
    				this.txtItemName6.Text   = strCurrentName;
    				this.txtUnitPrice6.Text  = strCurrentPrice;
    				this.txtQuantity6.Text   = "1";
    				this.txtSubTotal6.Text   = strCurrentPrice;
    			}
    		}
    	}
    
    	this.btnCalculate_Click(sender, e);
    }
  28. Execute the application and perform a few transactions
 

Home Copyright © 2005-2016, FunctionX