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: Creating the Application
|
|
- Start Microsoft Visual Studio .NET and create a new Windows
Application named MusicStore2
- To add a new form, on the main menu, click Project -> Windows
Form...
- Set the Name to DataCenter and press Enter
- To add a new form, on the main menu, click Project -> Windows Form...
- Set the Name to Categories and press Enter
- 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 |
|
- To add a new form, on the main menu, click Project -> Windows Form...
- Set the Name to ItemTypes and press Enter
- 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 |
|
- To add a new form, on the main menu, click Project -> Windows Form...
- Set the Name to NewStoreItem and press Enter
- 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 |
|
- 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;
}
|
- Return to the NewStoreItem form
- 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();
}
|
- Return to the NewStoreItem form and double-click the lower New button
- Implement the Click event as follows:
private void btnNewType_Click(object sender, System.EventArgs e)
{
ItemTypes frmTypes = new ItemTypes();
frmTypes.ShowDialog();
}
|
- To add a new form, on the main menu, click Project -> Add Windows
Form...
- Set the Name to PurchaseOrder and press Enter
- 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 |
|
- Double-click the NewItem button
- Implement its Click event as follows:
private void btnNewItem_Click(object sender, System.EventArgs e)
{
NewStoreItem frm = new NewStoreItem();
frm.ShowDialog();
}
|
- 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");
}
|
- 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 |
|
- 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();
}
|
- 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();
}
|
- 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();
}
|
- Save all
|
|