![]() |
Sample Data Set Application: |
|
Introduction |
|
This is a sample database used by a company that sells auto parts. It is called College Park Auto-Parts. This sample XML application explores the DataSet class of the .NET Framework. The necessary tables of the database are created as DataTable objects. Of course, each data table contains its columns that are based on the DataColumn class. Once the data set and its tables have been created, the user can create the records. These start with a list of the store items, which are mostly the auto parts sold in the store. When using the application, the user is then able to create customers orders. |
![]() |
|||||||||||||||
|
|||||||||||||||
![]() |
|||||||||||||||
|
|||||||||||||||
| Selected Columns: | HeaderText | DataPropertyName | ColumnType | DataSource | DisplayMember |
| Make | Make | Make | DataGridViewComboBoxColumn | dsMakes | Make.MakeName |
| Model | Model | Model |
![]() |
|||||||||||||||
|
|||||||||||||||
| 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 | Make.MakeName | 85 |
| Model | Model | Model | DataGridViewComboBoxColumn | dsModels | CarModel.Model | 130 |
| Category | Category | Category | DataGridViewComboBoxColumn | dsCategories | Category.CategoryName | 120 |
| Part Name | Part Name | PartName | 185 | |||
| Unit Price | Unit Price | UnitPrice | 65 |
![]() |
||||||||||||||||||||||||
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CollegeParkAutoParts4a
{
public partial class AutoParts : Form
{
public AutoParts()
{
InitializeComponent();
}
private void btnNewMake_Click(object sender, EventArgs e)
{
ListOfMakes frmMakes = new ListOfMakes();
frmMakes.ShowDialog();
}
}
}
|
private void btnNewModel_Click(object sender, EventArgs e)
{
ListOfModels frmModels = new ListOfModels();
frmModels.ShowDialog();
}
|
private void btnNewCategory_Click(object sender, EventArgs e)
{
ListOfCategories frmCategories = new ListOfCategories();
frmCategories.ShowDialog();
}
|
private void btnNewAutoPart_Click(object sender, EventArgs e)
{
AutoParts frmParts = new AutoParts();
if (frmParts.ShowDialog() == DialogResult.Cancel)
ShowAutoParts();
}
|
A record on a table is represented as a row (horizontal) of data. A row, or record, is an object based on the DataRow class. Before adding a new record to a table, you must let the table know. This is done by calling the DataTable.NewRow() method. Its syntax is: public DataRow NewRow(); The DataTable.NewRow() method returns a DataRow object.
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CollegeParkAutoParts4a
{
public partial class ListOfCategories : Form
{
public ListOfCategories()
{
InitializeComponent();
}
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);
dsCategories.WriteXml(strDirectory + "\\categories.xml");
Close();
}
}
}
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CollegeParkAutoParts4a
{
public partial class ListOfMakes : Form
{
public ListOfMakes()
{
InitializeComponent();
}
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();
}
}
}
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CollegeParkAutoParts4a
{
public partial class ListOfModels : Form
{
public ListOfModels()
{
InitializeComponent();
}
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);
dsModels.WriteXml(strDirectory + "\\models.xml");
Close();
}
}
}
|
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);
dsStoreItems.WriteXml(strDirectory + "\\StoreItems.xml");
Close();
}
|
| Acura |
| Audi |
| BMW |
| Buick |
| MDX |
| RDX |
| A4 |
| A6 |
| LaCrosse |
| Lucerne |
| Exhaust |
| Air Intake |
| Cooling System |
| Engine Electrical |
private void btnSave_Click(object sender, EventArgs e)
{
BinaryFormatter bfmCustomerOrder = new BinaryFormatter();
// We will store our files in the following folder
string strDirectory = @"C:\College Park Auto Parts\Receipts";
DirectoryInfo dirInfo = new DirectoryInfo(strDirectory);
string strFilename = strDirectory + "\\" + txtSave.Text + ".cap";
List<PartsOrdered> lstOrderedParts = null;
if (lvwSelectedParts.Items.Count == 0)
return;
else
{
lstOrderedParts = new List<PartsOrdered>();
for (int i = 0; i < lvwSelectedParts.Items.Count; i++)
{
PartsOrdered part = new PartsOrdered();
part.PartNumber = long.Parse(lvwSelectedParts.Items[i].Text);
part.PartName = lvwSelectedParts.Items[i].SubItems[1].Text;
part.UnitPrice = double.Parse(lvwSelectedParts.Items[i].SubItems[2].Text);
part.Quantity = int.Parse(lvwSelectedParts.Items[i].SubItems[3].Text);
part.SubTotal = double.Parse(lvwSelectedParts.Items[i].SubItems[4].Text);
lstOrderedParts.Add(part);
}
FileStream stmCustomerOrder = new FileStream(strFilename, FileMode.Create);
try
{
bfmCustomerOrder.Serialize(stmCustomerOrder, lstOrderedParts);
}
finally
{
stmCustomerOrder.Close();
}
}
}
|
private void ListOfCategories_Load(object sender, EventArgs e)
{
string strFilename = @"C:\College Park Auto Parts\categories.xml";
if (File.Exists(strFilename))
dsCategories.ReadXml(strFilename);
}
|
private void ListOfMakes_Load(object sender, EventArgs e)
{
string strFilename = @"C:\College Park Auto Parts\makes.xml";
if (File.Exists(strFilename))
dsMakes.ReadXml(strFilename);
}
|
private void ListOfModels_Load(object sender, EventArgs e)
{
string strFilename = @"C:\College Park Auto Parts\makes.xml";
if (File.Exists(strFilename))
dsMakes.ReadXml(strFilename);
strFilename = @"C:\College Park Auto Parts\models.xml";
if (File.Exists(strFilename))
dsModels.ReadXml(strFilename);
}
|
private void AutoParts_Load(object sender, EventArgs e)
{
string strFilename = @"C:\College Park Auto Parts\StoreItems.xml";
if( File.Exists(strFilename) )
dsStoreItems.ReadXml(strFilename);
}
|
private void AutoParts_Activated(object sender, EventArgs e)
{
string strFilename = @"C:\College Park Auto Parts\makes.xml";
if (File.Exists(strFilename))
dsMakes.ReadXml(strFilename);
strFilename = @"C:\College Park Auto Parts\models.xml";
if (File.Exists(strFilename))
dsModels.ReadXml(strFilename);
strFilename = @"C:\College Park Auto Parts\categories.xml";
if (File.Exists(strFilename))
dsCategories.ReadXml(strFilename);
}
|
Before performing any operation on a record, you must be able to locate it. That is, you must be able to identify a record among the various records of a table. The records of a table are stored in a list based on the DataRowCollection class. To locate a record in the DataTable.Rows collection, the DataRowCollection class has an indexed property that is defined as follows: public DataRow this[int index] {get;}
The first record has an index of 0. The second record has an index of 1, and so on.
|
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 strFilename = @"C:\College Park Auto Parts\StoreItems.xml";
if (File.Exists(strFilename))
{
dsStoreItems.ReadXml(strFilename);
// Add the makes to the years
foreach (TreeNode nodYear in nodRoot.Nodes)
{
List<string> lstMakes = new List<string>();
foreach (DataRow row in tblAutoPart.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 tblAutoPart.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 tblAutoPart.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 Central_Load(object sender, EventArgs e)
{
ShowAutoParts();
}
|
private void txtPartNumber_Leave(object sender, EventArgs e)
{
foreach (DataRow row in tblAutoPart.Rows)
{
if (row["PartNumber"].ToString() == txtPartNumber.Text)
{
txtPartName.Text = row["PartName"].ToString();
txtUnitPrice.Text = row["UnitPrice"].ToString();
txtQuantity.Text = "0";
txtSubTotal.Text = "0.00";
}
}
}
|
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");
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();
}
|
private void btnNewCustomerOrder_Click(object sender, EventArgs e)
{
int ReceiptNumber = 0;
string strFilename = @"C:\College Park Auto Parts\invoices.xml";
XmlDocument docInvoices = new XmlDocument();
if (File.Exists(strFilename))
{
docInvoices.Load(strFilename);
XmlElement elmRoot = docInvoices.DocumentElement;
XmlNodeList lstInvoices = elmRoot.ChildNodes;
XmlNodeList lstInvoiceNumbers =
elmRoot.GetElementsByTagName("ReceiptNumber");
ReceiptNumber = int.Parse(lstInvoiceNumbers[
lstInvoiceNumbers.Count - 1].InnerText);
}
txtSave.Text = (ReceiptNumber + 1).ToString();
txtTaxRate.Text = "5.75";
txtTaxAmount.Text = "0.00";
txtPartsTotal.Text = "0.00";
txtOrderTotal.Text = "0.00";
lvwSelectedParts.Items.Clear();
}
|
private void tvwAutoParts_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
TreeNode nodClicked = e.Node;
if (nodClicked.Level == 4)
lvwAutoParts.Items.Clear();
try
{
foreach (DataRow row in tblAutoPart.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);
}
}
}
catch (NullReferenceException)
{
}
}
|
private void btnSave_Click(object sender, EventArgs e)
{
// We will use a receipt number for each invoice
int ReceiptNumber = 0;
// This is the file that holds the invoices
string strFilename = @"C:\College Park Auto Parts\invoices.xml";
// Get a reference t
XmlDocument docInvoices = new XmlDocument();
// If at least one invoice had previously been created,
// then open the XML file that holds the invoices
if( File.Exists(strFilename) )
{
// Store the XML file structure into the DOM
docInvoices.Load(strFilename);
// Get a reference to the root element
XmlElement elmRoot = docInvoices.DocumentElement;
// Get a list of the receipt numbers
XmlNodeList lstInvoiceNumbers =
elmRoot.GetElementsByTagName("ReceiptNumber");
// Locate the last receipt number
ReceiptNumber = int.Parse(lstInvoiceNumbers[
lstInvoiceNumbers.Count - 1].InnerText);
}
else // if (!File.Exists(strFilename))
{
// If this is the first invoice to be created,
// set the receipt number to 1
ReceiptNumber = 1;
// Create the default XML structure
docInvoices.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<CustomersOrders></CustomersOrders>");
}
// 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>";
}
// Create an element named Invoice
XmlElement elmXml = docInvoices.CreateElement("Invoice");
string strInvoice = "<ReceiptNumber>" + txtSave.Text +
"</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
elmXml.InnerXml = strInvoice;
// Add the new invoice to the file
docInvoices.DocumentElement.AppendChild(elmXml);
// Save the XML file
docInvoices.Save(strFilename);
// Reset the customer order
ReceiptNumber = int.Parse(txtSave.Text);
txtSave.Text = (ReceiptNumber + 1).ToString();
txtTaxRate.Text = "5.75";
txtTaxAmount.Text = "0.00";
txtPartsTotal.Text = "0.00";
txtOrderTotal.Text = "0.00";
txtPartNumber.Text = "";
txtPartName.Text = "";
txtUnitPrice.Text = "0.00";
txtQuantity.Text = "0";
txtSubTotal.Text = "0.00";
lvwSelectedParts.Items.Clear();
}
|
private void btnOpen_Click(object sender, EventArgs e)
{
XmlDocument docInvoices = new XmlDocument();
string strFilename = @"C:\College Park Auto Parts\invoices.xml";
// Check that the file exists. If so, open it
if (File.Exists(strFilename))
{
// 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
docInvoices.Load(strFilename);
// Get a reference to the root element
XmlElement elmRoot = docInvoices.DocumentElement;
// Get a list of the invoices in the file
XmlNodeList lstInvoices = elmRoot.GetElementsByTagName("Invoice");
// Check each invoice
foreach (XmlNode nodInvoice in lstInvoices)
{
// Look for an invoice that has the same number
// as on the Open text box
if (nodInvoice["ReceiptNumber"].InnerText == txtOpen.Text)
{
// If you find it, make a note
found = true;
txtOpen.Text = nodInvoice["ReceiptNumber"].InnerText;
txtSave.Text = nodInvoice["ReceiptNumber"].InnerText;
// Retrieve the values of the invoice
// and display them on the form
try
{
foreach (XmlNode nodeReceipt in nodInvoice.ChildNodes)
{
XmlNode node =
nodeReceipt.NextSibling.NextSibling.ChildNodes[0];
ListViewItem lviInvoice =
new ListViewItem(node.InnerText);
lviInvoice.SubItems.Add(node.NextSibling.InnerText);
lviInvoice.SubItems.Add(
node.NextSibling.NextSibling.InnerText);
lviInvoice.SubItems.Add(
node.NextSibling.NextSibling.NextSibling.InnerText);
lviInvoice.SubItems.Add(
node.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
lvwSelectedParts.Items.Add(lviInvoice);
}
txtPartsTotal.Text = nodInvoice["PartsTotal"].InnerText;
txtTaxRate.Text = nodInvoice["TaxRate"].InnerText;
txtTaxAmount.Text = nodInvoice["TaxAmount"].InnerText;
txtOrderTotal.Text = nodInvoice["OrderTotal"].InnerText;
}
catch (NullReferenceException)
{
}
}
}
// If the invoice was not found, let the user know
if (found == false)
MessageBox.Show("There is no invlice with that receipt number");
}// IF the XML file was not found, let the user know
else
MessageBox.Show("The file " + strFilename + " was not found");
}
|


|
|
||
| Home | Copyright © 2007 FunctionX, Inc. | |
|
|
||