The DataSet of the .NET Framework is a very impressive
object. It allows you to create and manage any type of list. This makes 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
database, 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 comes to 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 Forms
Application named MusicStore2
- To add a new form, on the main menu, click Project -> Add New
Item...
- In the Templates list, click Windows Forms (.NET)
- Set the Name to DataCenter and press Enter
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- 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 -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- 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 -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- 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:
System::Void NewStoreItem_Load(System::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
- Return to the NewStoreItem form and double-click the lower New button
- In the top section of the file, under the #pragma line, type:
#include "Categories.h"
#include "ItemTypes.h"
|
- Implement the Click events as follows:
System::Void btnNewCategory_Click(System::Object * sender,
System::EventArgs * e)
{
Categories *frmCat = new Categories;
frmCat->ShowDialog();
}
System::Void btnNewType_Click(System::Object * sender,
System::EventArgs * e)
{
ItemTypes *frmTypes = new ItemTypes;
frmTypes->ShowDialog();
}
|
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- 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
Enabled: False |
Form |
|
|
CancelButton: btnClose
MaximizeBox: False
StartPosition: CenterScreen |
|
- Double-click the NewItem button
- In the top section of the file, under the #pragma line, type #include
"NewStoreItem.h"
- Implement its Click event as follows:
System::Void btnNewItem_Click(System::Object * sender, _
System::EventArgs * e)
{
NewStoreItem *frm = new NewStoreItem;
frm->ShowDialog();
}
|
- Double-click the Calculate Order button and implement its Click
event as follows:
System::Void btnCalculate_Click(System::Object * sender,
System::EventArgs * e)
{
double item1UnitPrice = 0.00, item2UnitPrice = 0.00, item3UnitPrice = 0.00,
item4UnitPrice = 0.00, item5UnitPrice = 0.00, item6UnitPrice = 0.00;
int quantity1 = 0, quantity2 = 0, quantity3 = 0,
quantity4 = 0, quantity5 = 0, quantity6 = 0;
double item1SubTotal = 0.00, item2SubTotal = 0.00, item3SubTotal = 0.00,
item4SubTotal = 0.00, item5SubTotal = 0.00, item6SubTotal = 0.00;
double totalOrder;
try {
item1UnitPrice = this->txtUnitPrice1->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice1->Text = S"0.00";
this->txtUnitPrice1->Focus();
}
try {
quantity1 = this->txtQuantity1->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity1->Text = S"0";
this->txtQuantity1->Focus();
}
try {
item2UnitPrice = this->txtUnitPrice2->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice2->Text = S"0.00";
this->txtUnitPrice2->Focus();
}
try {
quantity2 = this->txtQuantity2->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity2->Text = S"0";
this->txtQuantity2->Focus();
}
try {
item3UnitPrice = this->txtUnitPrice3->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice3->Text = S"0.00";
this->txtUnitPrice3->Focus();
}
try {
quantity3 = this->txtQuantity3->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity3->Text = S"0";
this->txtQuantity3->Focus();
}
try {
item4UnitPrice = this->txtUnitPrice4->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice4->Text = S"0.00";
this->txtUnitPrice4->Focus();
}
try {
quantity4 = this->txtQuantity4->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity4->Text = S"0";
this->txtQuantity4->Focus();
}
try {
item5UnitPrice = this->txtUnitPrice5->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice5->Text = S"0.00";
this->txtUnitPrice5->Focus();
}
try {
quantity5 = this->txtQuantity5->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity5->Text = S"0";
this->txtQuantity5->Focus();
}
try {
item6UnitPrice = this->txtUnitPrice6->Text->ToDouble(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Unit Price");
this->txtUnitPrice6->Text = S"0.00";
this->txtUnitPrice6->Focus();
}
try {
quantity6 = this->txtQuantity6->Text->ToInt16(0);
}
catch(FormatException *)
{
MessageBox::Show(S"Invalid Quantity");
this->txtQuantity6->Text = S"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(S"F");
this->txtSubTotal2->Text = item2SubTotal.ToString(S"F");
this->txtSubTotal3->Text = item3SubTotal.ToString(S"F");
this->txtSubTotal4->Text = item4SubTotal.ToString(S"F");
this->txtSubTotal5->Text = item5SubTotal.ToString(S"F");
this->txtSubTotal6->Text = item6SubTotal.ToString(S"F");
this->txtTotalOrder->Text = totalOrder.ToString(S"F");
}
|
- Display the first form (Form1.h [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
- Return to the form and double-click the New Purchase Order and the Close
buttons
- Implement the Click events as follows:
#pragma once
#include "NewStoreItem.h"
#include "PurchaseOrder.h"
namespace MusicStore2
{
. . . No Change
private: System::Void btnNewItem_Click(System::Object * sender,
System::EventArgs * e)
{
NewStoreItem *frm = new NewStoreItem;
frm->ShowDialog();
}
private: System::Void btnNewOrder_Click(System::Object * sender,
System::EventArgs * e)
{
PurchaseOrder *frm = new PurchaseOrder;
frm->ShowDialog();
}
private: System::Void btnClose_Click(System::Object * sender,
System::EventArgs * e)
{
Close();
}
};
}
|
- Save all
|
|