MSVC# - Data Sets Application: College Park Auto-Parts
MSVC# - Data Sets Application: College Park Auto-Parts
Introduction
A database is used to hold records. Most databases use records that depend on other records that in turn may depend on other records. Sometimes the scenarios become complex. One way to create and manage those records is to use a data set. To make the task easy, the .NET Framework includes a whose library for data sets. Up to Microsoft Visual Studio 2019, you can visually create the data set, its tables, the columns of those tables, and their records.
Practical Learning: Introducing Data Sets
The Vehicles Makes
The business of this application is to sell auto-parts. When a customer wants to purchase an auto part, the first relevant piece of information to provide is the make of the vehicle on which the part will be used.
Practical Learning: Introducing Vehicles Makes
|
using System.IO; namespace CollegeParkAutoParts1 { public partial class VehiclesMakes : Form { public Makes() { InitializeComponent(); } private void VehiclesMakes_Load(object sender, EventArgs e) { string fileName = @"C:\College Park Auto Parts\Makes.xml"; if (File.Exists(fileName)) dsMakes.ReadXml(fileName); } private void btnClose_Click(object sender, EventArgs e) { string strDirectory = @"C:\College Park Auto Parts"; // If this directory doesn't exist, create it Directory.CreateDirectory(strDirectory); dsMakes.WriteXml(strDirectory + "\\Makes.xml"); Close(); } } }
Vehicles Models
A vehicle is identified by its model. Our database already includes a table for that. We will just need a form to handle issues related to vehicles models.
Practical Learning: Introducing Vehicles Models
Selected Columns: | HeaderText | DataPropertyName | ColumnType | DataSource | DisplayMember |
Make | Make | Make | DataGridViewComboBoxColumn | dsMakes | VehicleMake.Make |
Model | Model | Model |
|
using System.IO; namespace CollegeParkAutoParts1 { public partial class VehiclesModels : Form { public Models() { InitializeComponent(); } private void VehiclesModels_Load(object sender, EventArgs e) { string fileName = @"C:\College Park Auto Parts\Makes.xml"; if (File.Exists(fileName)) dsMakes.ReadXml(fileName); fileName = @"C:\College Park Auto Parts\Models.xml"; if (File.Exists(fileName)) dsModels.ReadXml(fileName); } private void btnClose_Click(object sender, EventArgs e) { string strDirectory = @"C:\College Park Auto Parts"; Directory.CreateDirectory(strDirectory); dsModels.WriteXml(strDirectory + "\\Models.xml"); Close(); } } }
Items Categories
To further identify a part of a vehicle, those parts are put in categories, and there are many of those categories. In our application, we will just list the categories based on our opinions.
Practical Learning: Setting Items Categories
|
using System.IO; namespace CollegeParkAutoParts1 { public partial class Categories : Form { public Categories() { InitializeComponent(); } private void Categories_Load(object sender, EventArgs e) { string fileName = @"C:\College Park Auto Parts\categories.xml"; if (File.Exists(fileName)) dsCategories.ReadXml(fileName); } private void btnClose_Click(object sender, EventArgs e) { string strDirectory = @"C:\College Park Auto Parts"; Directory.CreateDirectory(strDirectory); dsCategories.WriteXml(strDirectory + "\\categories.xml"); Close(); } } }
Store Items Inventory
The primary purpose of the business of our application is to sell auto parts. So it is necessary to create records for those parts. To make this happen, we had already created a table for auto parts. Now we need a graphical object, a form, to manager the records.
Practical Learning: Preparing an Items Inventory
ColumnName | (Name) |
PartNumber | colPartNumber |
Year | colYear |
Make | colMake |
Model | colModel |
Category | colCategory |
PartName | colPartName |
UnitPrice | colUnitPrice |
Selected Columns: | HeaderText | DataPropertyName | ColumnType | DataSource | DisplayMember | Width |
Part # | Part # | PartNumber | 50 | |||
Year | Year | Year | 40 | |||
Make | Make | Make | DataGridViewComboBoxColumn | dsMakes | VehicleMake.Make | 85 |
Model | Model | Model | DataGridViewComboBoxColumn | dsModels | VehicleModel.Model | 130 |
Category | Category | Category | DataGridViewComboBoxColumn | dsCategories | PartCategory.Category | 120 |
Part Name | Part Name/Description | PartName | 185 | |||
Unit Price | Unit Price | UnitPrice | 65 |
using System.IO; namespace CollegeParkAutoParts1 { public partial class AutoParts : Form { public AutoParts() { InitializeComponent(); } private void AutoParts_Load(object sender, EventArgs e) { string fileName = @"C:\College Park Auto Parts\StoreItems.xml"; if (File.Exists(fileName)) dsStoreItems.ReadXml(fileName); } private void AutoParts_Activated(object sender, EventArgs e) { string fileName = @"C:\College Park Auto Parts\makes.xml"; if (File.Exists(fileName)) dsMakes.ReadXml(fileName); fileName = @"C:\College Park Auto Parts\models.xml"; if (File.Exists(fileName)) dsModels.ReadXml(fileName); fileName = @"C:\College Park Auto Parts\categories.xml"; if (File.Exists(fileName)) dsCategories.ReadXml(fileName); } private void btnNewMake_Click(object sender, EventArgs e) { Makes frmMakes = new Makes(); frmMakes.ShowDialog(); } private void btnNewModel_Click(object sender, EventArgs e) { Models frmModels = new Models(); frmModels.ShowDialog(); } private void btnNewCategory_Click(object sender, EventArgs e) { Categories frmCategories = new Categories(); frmCategories.ShowDialog(); } private void btnClose_Click(object sender, EventArgs e) { string strDirectory = @"C:\College Park Auto Parts"; Directory.CreateDirectory(strDirectory); dsStoreItems.WriteXml(strDirectory + "\\StoreItems.xml"); Close(); } } }
Like most graphical applications, ours will need a central point. This is a form from which a user will select the year of a vehicle, the make, and the model. Then the user will select the category of the part. A section of the form will show the list of parts that respond to the previously mentioned criteria. The user can then select the desired part(s) and process a customer's order.
Practical Learning: Managing the Business
ColumnName | (Name) |
PartNumber | colPartNumber |
Year | colYear |
Make | colMake |
Model | colModel |
Category | colCategory |
PartName | colPartName |
UnitPrice | colUnitPrice |
|
using System.IO; using System.Xml; using System.Data; using System.Collections.Generic; namespace CollegeParkAutoRepair1 { public partial class CollegeParkAutoRepair : Form { public CollegeParkAutoRepair() { InitializeComponent(); } private void tvwAutoParts_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode nodClicked = e.Node; if (nodClicked.Level == 4) lvwAutoParts.Items.Clear(); try { foreach (DataRow row in dtStoreItem.Rows) { if ((row["Category"].ToString() == nodClicked.Text) && (row["Model"].ToString() == nodClicked.Parent.Text) && (row["Make"].ToString() == nodClicked.Parent.Parent.Text) && (row["Year"].ToString() == nodClicked.Parent.Parent.Parent.Text)) { ListViewItem lviAutoPart = new ListViewItem(row["PartNumber"].ToString()); lviAutoPart.SubItems.Add(row["PartName"].ToString()); lviAutoPart.SubItems.Add(row["UnitPrice"].ToString()); lvwAutoParts.Items.Add(lviAutoPart); break; } } } catch (NullReferenceException nre) { MessageBox.Show("The application produced an error as follows: " + Environment.NewLine + nre.Message, "College Park Auto-Parts", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void lvwAutoParts_DoubleClick(object sender, EventArgs e) { ListViewItem lviAutoPart = lvwAutoParts.SelectedItems[0]; if ((lvwAutoParts.SelectedItems.Count == 0) || (lvwAutoParts.SelectedItems.Count > 1)) return; txtPartNumber.Text = lviAutoPart.Text; txtPartName.Text = lviAutoPart.SubItems[1].Text; txtUnitPrice.Text = lviAutoPart.SubItems[2].Text; txtQuantity.Text = "1"; txtSubTotal.Text = lviAutoPart.SubItems[2].Text; txtQuantity.Focus(); } private void txtPartNumber_Leave(object sender, EventArgs e) { foreach (DataRow row in dtStoreItem.Rows) { if (row["PartNumber"].ToString() == txtPartNumber.Text) { txtPartName.Text = row["PartName"].ToString(); txtUnitPrice.Text = row["UnitPrice"].ToString(); txtQuantity.Text = "1"; txtSubTotal.Text = row["UnitPrice"].ToString(); txtQuantity.Focus(); break; } } } private void txtUnitPrice_Leave(object sender, EventArgs e) { int quantity = 0; double unitPrice = 0.00D; try { unitPrice = double.Parse(txtUnitPrice.Text); } catch (FormatException) { MessageBox.Show("Invalid Unit Price!", "College Park Auto-Parts"); } try { quantity = int.Parse(txtQuantity.Text); } catch (FormatException) { MessageBox.Show("Invalid Quandtity!", "College Park Auto-Parts"); } double subTotal = unitPrice * quantity; txtSubTotal.Text = subTotal.ToString("F"); } internal void CalculateOrder() { // Calculate the current total order and update the order double taxRate = 0.00d; double partsTotal = 0.00D; ListViewItem lviSelectedPart = lvwSelectedParts.Items[0]; foreach (ListViewItem lvi in lvwSelectedParts.Items) { ListViewItem.ListViewSubItem SubItem = lvi.SubItems[4]; partsTotal += double.Parse(SubItem.Text); } try { taxRate = double.Parse(txtTaxRate.Text) / 100; } catch (FormatException) { MessageBox.Show("Invalid Tax Rate"); } double taxAmount = partsTotal * taxRate; double orderTotal = partsTotal + taxAmount; txtPartsTotal.Text = partsTotal.ToString("F"); txtTaxAmount.Text = taxAmount.ToString("F"); txtOrderTotal.Text = orderTotal.ToString("F"); } private void lvwSelectedParts_DoubleClick(object sender, EventArgs e) { ListViewItem lviSelectedPart = lvwSelectedParts.SelectedItems[0]; if ((lvwSelectedParts.SelectedItems.Count == 0) || (lvwSelectedParts.SelectedItems.Count > 1)) return; txtPartNumber.Text = lviSelectedPart.Text; txtPartName.Text = lviSelectedPart.SubItems[1].Text; txtUnitPrice.Text = lviSelectedPart.SubItems[2].Text; txtQuantity.Text = lviSelectedPart.SubItems[3].Text; txtSubTotal.Text = lviSelectedPart.SubItems[4].Text; lvwSelectedParts.Items.Remove(lviSelectedPart); CalculateOrder(); } private void btnAdd_Click(object sender, EventArgs e) { if (txtPartNumber.Text.Length == 0) { MessageBox.Show("There is no part to be added to the order.", "College Park Auto-Parts"); return; } ListViewItem lviSelectedPart = new ListViewItem(txtPartNumber.Text); lviSelectedPart.SubItems.Add(txtPartName.Text); lviSelectedPart.SubItems.Add(txtUnitPrice.Text); lviSelectedPart.SubItems.Add(txtQuantity.Text); lviSelectedPart.SubItems.Add(txtSubTotal.Text); lvwSelectedParts.Items.Add(lviSelectedPart); CalculateOrder(); } internal void ShowAutoParts() { tvwAutoParts.Nodes.Clear(); TreeNode nodRoot = tvwAutoParts.Nodes.Add("College Park Auto-Parts", "College Park Auto-Parts", 0, 1); // Show the years nodes for (int years = DateTime.Today.Year + 1; years >= 1960; years--) nodRoot.Nodes.Add(years.ToString(), years.ToString(), 2, 3); tvwAutoParts.SelectedNode = nodRoot; // Expand the root node tvwAutoParts.ExpandAll(); // This is the file that holds the list of store items on sale string fileName = @"C:\College Park Auto Parts\StoreItems.xml"; if (File.Exists(fileName)) { dsStoreItems.ReadXml(fileName); // Add the makes to the years foreach (TreeNode nodYear in nodRoot.Nodes) { List<string> lstMakes = new List<string>(); foreach (DataRow row in dtStoreItem.Rows) { if (nodYear.Text == row["Year"].ToString()) { if (!lstMakes.Contains(row["Make"].ToString())) lstMakes.Add(row["Make"].ToString()); } } foreach (string strMake in lstMakes) nodYear.Nodes.Add(strMake, strMake, 4, 5); } // Add the models to the makes foreach (TreeNode nodYear in nodRoot.Nodes) { foreach (TreeNode nodMake in nodYear.Nodes) { List<string> lstModels = new List<string>(); foreach (DataRow row in dtStoreItem.Rows) { if ((nodYear.Text == row["Year"].ToString()) && (nodMake.Text == row["Make"].ToString())) { if (!lstModels.Contains(row["Model"].ToString())) lstModels.Add(row["Model"].ToString()); } } foreach (string strModel in lstModels) nodMake.Nodes.Add(strModel, strModel, 6, 7); } } // Show the categories nodes foreach (TreeNode nodYear in nodRoot.Nodes) { foreach (TreeNode nodMake in nodYear.Nodes) { foreach (TreeNode nodModel in nodMake.Nodes) { List<string> lstCategories = new List<string>(); foreach (DataRow row in dtStoreItem.Rows) { if ((nodYear.Text == row["Year"].ToString()) && (nodMake.Text == row["Make"].ToString()) && (nodModel.Text == row["Model"].ToString())) { if (!lstCategories.Contains(row["Category"].ToString())) lstCategories.Add(row["Category"].ToString()); } } foreach (string strCategory in lstCategories) nodModel.Nodes.Add(strCategory, strCategory, 8, 9); } } } } } private void btnAutoParts_Click(object sender, EventArgs e) { AutoParts frmParts = new AutoParts(); if (frmParts.ShowDialog() == DialogResult.Cancel) ShowAutoParts(); } private void btnNewCustomerOrder_Click(object sender, EventArgs e) { int receiptNumber = 0; string fileName = @"C:\College Park Auto Parts\Receipts.xml"; XmlDocument DOMReceipts = new XmlDocument(); if (File.Exists(fileName)) { DOMReceipts.Load(fileName); XmlElement rootReceipts = DOMReceipts.DocumentElement; XmlNodeList listOfReceiptNumbers = rootReceipts.GetElementsByTagName("ReceiptNumber"); receiptNumber = int.Parse(ListOfReceiptNumbers[ listOfReceiptNumbers.Count - 1].InnerText); } txtSave.Text = (receiptNumber + 1).ToString(); txtTaxRate.Text = "5.75"; txtTaxAmount.Text = "0.00"; txtPartsTotal.Text = "0.00"; txtOrderTotal.Text = "0.00"; lvwAutoParts.Items.Clear(); lvwSelectedParts.Items.Clear(); } private void CollegeParkAutoParts_Load(object sender, EventArgs e) { // This is the directory in which the records of the database of this application will be saved string strDirectory = @"C:\College Park Auto Parts"; // If this directory doesn't exist, create it Directory.CreateDirectory(strDirectory); ShowAutoParts(); btnNewCustomerOrder_Click(sender, e); } private void btnSave_Click(object sender, EventArgs e) { // This is the file that holds the receipts string fileName = @"C:\College Park Auto Parts\Receipts.xml"; // Get a reference to the root XmlDocument DOMReceipts = new XmlDocument(); // We will create a list of all the parts // that the customer wants to purchase string strParts = ""; foreach (ListViewItem lviPart in lvwSelectedParts.Items) { strParts = strParts + "<Part>"; strParts = strParts + "<PartNumber>" + lviPart.SubItems[0].Text + "</PartNumber>"; strParts = strParts + "<PartName>" + lviPart.SubItems[1].Text + "</PartName>"; strParts = strParts + "<UnitPrice>" + lviPart.SubItems[2].Text + "</UnitPrice>"; strParts = strParts + "<Quantity>" + lviPart.SubItems[3].Text + "</Quantity>"; strParts = strParts + "<SubTotal>" + lviPart.SubItems[4].Text + "</SubTotal>"; strParts = strParts + "</Part>"; } // If this is the first customer order, ... if (!File.Exists(fileName)) { // If this is the first receipt to be created, // set the receipt number to 1 // and create the structure of the document DOMReceipts.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<CustomersOrders>" + "<Receipt>" + "<ReceiptNumber>1</ReceiptNumber>" + strParts + "<PartsTotal>" + txtPartsTotal.Text + "</PartsTotal><TaxRate>" + txtTaxRate.Text + "</TaxRate><TaxAmount>" + txtTaxAmount.Text + "</TaxAmount><OrderTotal>" + txtOrderTotal.Text + "</OrderTotal></Receipt></CustomersOrders>"); // Save the XML file DOMReceipts.Save(fileName); // Reset the customer order txtSave.Text = "2"; txtOpen.Text = "1"; txtTaxRate.Text = "5.75"; txtTaxAmount.Text = "0.00"; txtPartsTotal.Text = "0.00"; txtOrderTotal.Text = "0.00"; lvwSelectedParts.Items.Clear(); } // If this is not the first customer order, ... else // if (File.Exists(fileName)) { XmlNode nodeCustomerOrder = null; // We will use a receipt number for each receipt int receiptNumber = int.Parse(txtSave.Text); // If at least one receipt had previously been created, // then open the XML file that holds the receipts // Store the XML file structure into the DOM DOMReceipts.Load(fileName); // Get a reference to the root element XmlElement rootCustomersOrders = DOMReceipts.DocumentElement; // Get a list of the receipt numbers XmlNodeList listOfReceipts = rootCustomersOrders.GetElementsByTagName("Receipt"); // Check each receipt foreach (XmlNode nodeCurrentReceipt in listOfReceipts) { // Look for a receipt that has the same number // as in the Save text box if (nodeCurrentReceipt["ReceiptNumber"].InnerText == txtSave.Text) { // If you find it, reserve it nodeCustomerOrder = nodeCurrentReceipt; break; } } // Locate the last receipt number receiptNumber = int.Parse(txtSave.Text); // Create an element named Receipt XmlElement ElementReceipt = DOMReceipts.CreateElement("Receipt"); string strReceipt = "<ReceiptNumber>" + receiptNumber.ToString() + "</ReceiptNumber>" + strParts + "<PartsTotal>" + txtPartsTotal.Text + "</PartsTotal><TaxRate>" + txtTaxRate.Text + "</TaxRate><TaxAmount>" + txtTaxAmount.Text + "</TaxAmount><OrderTotal>" + txtOrderTotal.Text + "</OrderTotal>"; // Create the XML code of the new element ElementReceipt.InnerXml = strReceipt; // If this is a new customer order if (nodeCustomerOrder == null) { // Add the new receipt to the file DOMReceipts.DocumentElement.AppendChild(ElementReceipt); } // If the customer order existed already, we will only update it else // if (NodeCustomerOrder != null) { // Replace the existing customer order with the current one DOMReceipts.DocumentElement.ReplaceChild(ElementReceipt, nodeCustomerOrder); } // Save the XML file DOMReceipts.Save(fileName); // Reset the customer order btnNewCustomerOrder_Click(sender, e); } } private void btnOpen_Click(object sender, EventArgs e) { XmlDocument DOMReceipts = new XmlDocument(); string fileName = @"C:\College Park Auto Parts\Receipts.xml"; // Check that the file exists. If so, open it if (File.Exists(fileName)) { // This variable will allow us to know if we have the receipt number bool found = false; // Empty the list of selected parts lvwSelectedParts.Items.Clear(); // After opening the XML file, store it in the DOM DOMReceipts.Load(fileName); // Get a reference to the root element XmlElement rootReceipts = DOMReceipts.DocumentElement; // Get a list of the receipts in the file XmlNodeList listOfReceipts = rootReceipts.GetElementsByTagName("Receipt"); // Check each receipt foreach (XmlNode nodReceipt in listOfReceipts) { // Look for an receipt that has the same number // as on the Open text box if (nodReceipt["ReceiptNumber"].InnerText == txtOpen.Text) { // If you find it, make a note found = true; //txtOpen.Text = nodReceipt["ReceiptNumber"].InnerText; //txtSave.Text = nodReceipt["ReceiptNumber"].InnerText; // Retrieve the values of the receipt // and display them on the form try { foreach (XmlNode nodeSelectedParts in nodReceipt.ChildNodes) { XmlNode node = nodeSelectedParts.NextSibling.ChildNodes[0]; ListViewItem lviReceipt = new ListViewItem(node.InnerText); lviReceipt.SubItems.Add(node.NextSibling.InnerText); lviReceipt.SubItems.Add(node.NextSibling.NextSibling.InnerText); lviReceipt.SubItems.Add(node.NextSibling.NextSibling.NextSibling.InnerText); lviReceipt.SubItems.Add(node.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); lvwSelectedParts.Items.Add(lviReceipt); } } catch (NullReferenceException /* nre */) { /* MessageBox.Show("The application produced an error as follows: " + Environment.NewLine + nre.Message, "College Park Auto-Parts", MessageBoxButtons.OK, MessageBoxIcon.Information); */ } txtPartsTotal.Text = nodReceipt["PartsTotal"].InnerText; txtTaxRate.Text = nodReceipt["TaxRate"].InnerText; txtTaxAmount.Text = nodReceipt["TaxAmount"].InnerText; txtOrderTotal.Text = nodReceipt["OrderTotal"].InnerText; break; } } // If the receipt was not found, let the user know if (found == false) { MessageBox.Show("There is no customer order with that receipt number", "College Park Auto-Parts"); } }// IF the XML file was not found, let the user know else MessageBox.Show("The file " + fileName + " was not found", "College Park Auto-Parts"); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Ford |
Dodge |
Toyota |
Honda |
Ford | Escort SE L4 2.0 |
Dodge | Caravan SE L4 2.4 |
Toyota | Rav4 2WD/4-Door |
Honda | Civic 1.7 EX 4DR |
Ford | Taurus LX V6 3.0 |
Honda | Accord 2.3 LX 4DR |
Exhaust |
Air Intake |
Fuel Injection |
Cooling System |
Engine Electrical |
|
|||
Home | Copyright © 2005-2023, FunctionX | Wednesday 11 May 2022 | Home |
|