|
Application Authentication |
|
|
Application authentication consists of restricting access to an
application. You can first create an application and take care of authentication
later, but it is better to design the concept before starting. In this series of
articles, we are going to create a complete application, step by step, and in
different sections that each deals with one particular issue.
|
Imagine you want to create an application for a department
store. One of
the things you can take care of, as far as people who access the program are
concerned, is authentication.
In this example, we will create an application for
file processing. We will use either Microsoft Visual Studio 2010
Professional or Microsoft Visual C# 2010 Express.
Practical
Learning: Creating a Folder for the Application
|
|
- Log on to the server or the computer that will hold the records for the
application
- Create a folder named Fun Department Store
- Right-click that folder and click Share
- Click the arrow of the combo box in the File Sharing window and select
Everyone
- Click Add
- Click the down-pointing arrow on the right side of Everyone:
- If you are sharing from Microsoft Windows Server 2008, select
Contributor
- If you are sharing from Microsoft Windows 7, select Read/Write
- Click Share
- Click Close
Practical
Learning: Starting the Project
|
|
- Launch either Microsoft Visual C# Express or Microsoft Visual Studio
- To start an application, on the main menu, click File -> New Project ...
- If you are using Microsoft Visual Studio, in the left list, click Visual C#.
In the middle list, click Empty Project
- Set the Name to FunDS1
- Click OK
- On the main menu, click Project -> FunDepartmentStore1 Properties...
- In the Output Type combo box, select Windows Application
- Close the Properties window
- On the main menu, click Project -> Add Reference...
- In the Add Reference dialog box, click .NET
- In the list view, click System.Data
- Press and hold Ctrl
- Click System
- Click System.Drawing
- Click System.Windows.Forms
- Click System.Xml
- Release Ctrl
- Click OK
- To save the project, on the Standard toolbar, click the Save All button
- Accept the suggested path but make a note of it (you will need it) and
click OK
- To create a file for the project, on the main menu, click Project -> Add
New Item...
- In the middle list, click Code File
- Set the name to Employee
- Click Add
- In the empty document, type the following:
using System;
[Serializable]
public class Employee
{
public string EmployeeNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public bool Manager { get; set; }
public double HourlySalary { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
- To create a file for the project, on the main menu, click Project -> Add
New Item...
- In the middle list, click Code File
- Set the name to StoreItem
- Click Add
- In the empty document, type the following:
using System;
[Serializable]
public class StoreItem
{
public string ItemNumber { get; set; }
public DateTime ArrivalDate { get; set; }
public string Manufacturer { get; set; }
public string Category { get; set; }
public string SubCategory { get; set; }
public string ItemName { get; set; }
public string ItemSize { get; set; }
public double UnitPrice { get; set; }
public double DiscountRate { get; set; }
public string SaleStatus { get; set; }
}
- To create a new file, on the main menu, click Project -> Add File...
- In the middle list, click Code File
- Set the Name to StoreInventory
- Click Add
- In the empty document, type the following:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
public class StoreInventory : Form
{
private ColumnHeader colIndex;
private ColumnHeader colItemNumber;
private ColumnHeader colArrivalDate;
private ColumnHeader colManufacturer;
private ColumnHeader colCategory;
private ColumnHeader colSubCategory;
private ColumnHeader colItemName;
private ColumnHeader colItemSize;
private ColumnHeader colUnitPrice;
private ColumnHeader colSaleStatus;
private ListView lvwStoreItems;
private Button btnClose;
public StoreInventory()
{
InitializeComponent();
}
private void InitializeComponent()
{
colIndex = new ColumnHeader();
colItemNumber = new ColumnHeader();
colArrivalDate = new ColumnHeader();
colManufacturer = new ColumnHeader();
colCategory = new ColumnHeader();
colSubCategory = new ColumnHeader();
colItemName = new ColumnHeader();
colItemSize = new ColumnHeader();
colUnitPrice = new ColumnHeader();
colSaleStatus = new ColumnHeader();
lvwStoreItems = new ListView();
btnClose = new Button();
SuspendLayout();
colIndex.Text = "Index";
colIndex.Width = 40;
colItemNumber.Text = "Item #";
colItemNumber.TextAlign = HorizontalAlignment.Center;
colItemNumber.Width = 50;
colArrivalDate.Text = "Arrival Date";
colArrivalDate.Width = 70;
colManufacturer.Text = "Manufacturer";
colManufacturer.Width = 100;
colCategory.Text = "Category";
colSubCategory.Text = "Sub-Category";
colSubCategory.Width = 80;
colItemName.Text = "Item Name/Description";
colItemName.Width = 220;
colItemSize.Text = "Size";
colItemSize.TextAlign = HorizontalAlignment.Center;
colUnitPrice.Text = "Unit Price";
colUnitPrice.TextAlign = HorizontalAlignment.Right;
colSaleStatus.Text = "Status";
colSaleStatus.Width = 70;
btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnClose.Location = new Point(772, 182);
btnClose.Size = new Size(75, 23);
btnClose.TabIndex = 33;
btnClose.Text = "Close";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += new System.EventHandler(btnCloseClicked);
lvwStoreItems.Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
AnchorStyles.Left | AnchorStyles.Right;
lvwStoreItems.Columns.AddRange(new ColumnHeader[] {
colIndex, colItemNumber, colArrivalDate, colManufacturer,
colCategory, colSubCategory, colItemName,
colItemSize, colUnitPrice, colSaleStatus});
lvwStoreItems.FullRowSelect = true;
lvwStoreItems.GridLines = true;
lvwStoreItems.Location = new Point(12, 12);
lvwStoreItems.Size = new Size(835, 160);
lvwStoreItems.TabIndex = 32;
lvwStoreItems.UseCompatibleStateImageBehavior = false;
lvwStoreItems.View = System.Windows.Forms.View.Details;
ClientSize = new Size(859, 213);
Controls.Add(btnClose);
Controls.Add(lvwStoreItems);
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Fun Department Store - Current Store Inventory";
Load += new System.EventHandler(StoreInventoryLoaded);
ResumeLayout(false);
}
private void ShowInventory()
{
}
private void StoreInventoryLoaded(object sender, EventArgs e)
{
ShowInventory();
}
private void btnCloseClicked(object sender, EventArgs e)
{
Close();
}
}
- To create a new file, on the main menu, click Project -> Add New Item...
- In the middle list, click Code File
- Set the Name to Switchboard
- Click Add
- In the empty document, type the following:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
public class Switchboard : Form
{
private Button btnStoreInventory;
private Button btnCreateStoreItem;
private Button btnEmployees;
private Button btnLogInUser;
private Button btnClose;
public Switchboard()
{
InitializeComponent();
}
private void InitializeComponent()
{
btnStoreInventory = new Button();
btnCreateStoreItem = new Button();
btnEmployees = new Button();
btnLogInUser = new Button();
btnClose = new Button();
SuspendLayout();
btnStoreInventory.Font = new Font("Georgia", 18F,
FontStyle.Bold, GraphicsUnit.Point, 0);
btnStoreInventory.Location = new Point(12, 12);
btnStoreInventory.Size = new Size(227, 98);
btnStoreInventory.TabIndex = 23;
btnStoreInventory.Text = "View Store Inventory";
btnStoreInventory.UseVisualStyleBackColor = true;
btnStoreInventory.Click +=
new System.EventHandler(btnStoreInventoryClicked);
btnCreateStoreItem.Font = new Font("Georgia", 18F,
FontStyle.Bold, GraphicsUnit.Point, 0);
btnCreateStoreItem.Location = new Point(255, 13);
btnCreateStoreItem.Size = new Size(227, 98);
btnCreateStoreItem.TabIndex = 25;
btnCreateStoreItem.Text = "Create Store Item";
btnCreateStoreItem.UseVisualStyleBackColor = true;
btnCreateStoreItem.Click +=
new System.EventHandler(btnCreateStoreItemClicked);
btnEmployees.Font = new Font("Georgia", 18F,
FontStyle.Bold, GraphicsUnit.Point, 0);
btnEmployees.Location = new Point(12, 128);
btnEmployees.Size = new Size(227, 98);
btnEmployees.TabIndex = 27;
btnEmployees.Text = "Employees";
btnEmployees.UseVisualStyleBackColor = true;
btnEmployees.Click +=
new System.EventHandler(btnEmployeesClicked);
btnLogInUser.Font = new Font("Georgia", 18F,
FontStyle.Bold, GraphicsUnit.Point, 0);
btnLogInUser.Location = new Point(255, 128);
btnLogInUser.Size = new Size(227, 98);
btnLogInUser.TabIndex = 26;
btnLogInUser.Text = "Log in as a Different Employee";
btnLogInUser.UseVisualStyleBackColor = true;
btnLogInUser.Click +=
new System.EventHandler(btnLogInUserClicked);
btnClose.Font = new Font("Georgia", 18F,
FontStyle.Bold, GraphicsUnit.Point, 0);
btnClose.Location = new Point(11, 241);
btnClose.Size = new Size(471, 67);
btnClose.TabIndex = 24;
btnClose.Text = "Close Application";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += new System.EventHandler(btnCloseClicked);
ClientSize = new Size(494, 319);
Controls.Add(btnEmployees);
Controls.Add(btnLogInUser);
Controls.Add(btnCreateStoreItem);
Controls.Add(btnClose);
Controls.Add(btnStoreInventory);
Name = "Switchboard";
StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
Text = "Fun Department Store";
ResumeLayout(false);
}
private void btnStoreInventoryClicked(object sender, EventArgs e)
{
StoreInventory si = new StoreInventory();
si.ShowDialog();
}
private void btnCreateStoreItemClicked(object sender, EventArgs e)
{
}
private void btnEmployeesClicked(object sender, EventArgs e)
{
}
private void btnLogInUserClicked(object sender, EventArgs e)
{
}
private void btnCloseClicked(object sender, EventArgs e)
{
Close();
}
}
public class DepartmentStore
{
public static int Main()
{
Application.Run(new Switchboard());
return 0;
}
}
- To execute the application, press F5
- Click the View Store Inventory button
- Close the Store Inventory form
- Close the Switchboard form
- To add a new file, on the main menu, click Project -> New Item...
- In the middle list, click Code File
- Change the Name to CreateStoreItem
- Click Add
- In the empty document, type the following:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class CreateStoreItem : Form
{
private Label lblItemNumber;
private TextBox txtItemNumber;
private Label lblArrivalDate;
private MaskedTextBox txtArrivalDate;
private Label lblManufacturer;
private TextBox txtManufacturer;
private Label lblCategory;
private ComboBox cbxCategories;
private Label lblSubCategory;
private ComboBox cbxSubCategories;
private Label lblItemName;
private TextBox txtItemName;
private Label lblItemSize;
private TextBox txtItemSize;
private Label lblUnitPrice;
private TextBox txtUnitPrice;
private Label lblDiscountRate;
private TextBox txtDiscountRate;
private Label lblPercent;
private Label lblSaleStatus;
private ComboBox cbxSaleStatus;
private Button btnReset;
private Button btnCreate;
private Button btnClose;
public CreateStoreItem()
{
InitializeComponent();
}
private void InitializeComponent()
{
lblItemNumber = new Label();
txtItemNumber = new TextBox();
lblArrivalDate = new Label();
txtArrivalDate = new MaskedTextBox();
lblManufacturer = new Label();
txtManufacturer = new TextBox();
lblCategory = new Label();
cbxCategories = new ComboBox();
lblSubCategory = new Label();
cbxSubCategories = new ComboBox();
lblItemName = new Label();
txtItemName = new TextBox();
lblItemSize = new Label();
txtItemSize = new TextBox();
lblUnitPrice = new Label();
txtUnitPrice = new TextBox();
txtDiscountRate = new TextBox();
lblDiscountRate = new Label();
lblPercent = new Label();
lblSaleStatus = new Label();
cbxSaleStatus = new ComboBox();
btnClose = new Button();
btnCreate = new Button();
btnReset = new Button();
SuspendLayout();
lblItemNumber.AutoSize = true;
lblItemNumber.Location = new Point(11, 14);
lblItemNumber.Size = new Size(40, 13);
lblItemNumber.TabIndex = 0;
lblItemNumber.Text = "Item #:";
txtItemNumber.Location = new Point(95, 11);
txtItemNumber.Size = new Size(96, 20);
txtItemNumber.TabIndex = 1;
lblArrivalDate.AutoSize = true;
lblArrivalDate.Location = new Point(223, 14);
lblArrivalDate.Size = new Size(65, 13);
lblArrivalDate.TabIndex = 2;
lblArrivalDate.Text = "Arrival Date:";
txtArrivalDate.Location = new Point(303, 11);
txtArrivalDate.Mask = "00/00/0000";
txtArrivalDate.Size = new Size(121, 20);
txtArrivalDate.TabIndex = 3;
txtArrivalDate.ValidatingType = typeof(System.DateTime);
lblManufacturer.AutoSize = true;
lblManufacturer.Location = new Point(11, 43);
lblManufacturer.Size = new Size(73, 13);
lblManufacturer.TabIndex = 4;
lblManufacturer.Text = "Manufacturer:";
txtManufacturer.Location = new Point(95, 40);
txtManufacturer.Size = new Size(330, 20);
txtManufacturer.TabIndex = 5;
lblCategory.AutoSize = true;
lblCategory.Location = new Point(11, 72);
lblCategory.Size = new Size(52, 13);
lblCategory.TabIndex = 6;
lblCategory.Text = "Category:";
cbxCategories.FormattingEnabled = true;
cbxCategories.Items.AddRange(new object[] {
"Men", "Girls", "Boys",
"Babies", "Women", "Other"});
cbxCategories.Location = new Point(95, 70);
cbxCategories.Size = new Size(116, 21);
cbxCategories.TabIndex = 7;
lblSubCategory.AutoSize = true;
lblSubCategory.Location = new Point(223, 73);
lblSubCategory.Size = new Size(74, 13);
lblSubCategory.TabIndex = 8;
lblSubCategory.Text = "Sub-Category:";
cbxSubCategories.FormattingEnabled = true;
cbxSubCategories.Items.AddRange(new object[] {
"Skirts", "Pants", "Shirts", "Shoes",
"Blouse", "Beauty", "Dresses", "Clothing",
"Sweater", "Watches", "Handbags", "Miscellaneous"});
cbxSubCategories.Location = new Point(303, 70);
cbxSubCategories.Size = new Size(121, 21);
cbxSubCategories.TabIndex = 9;
lblItemName.AutoSize = true;
lblItemName.Location = new Point(11, 104);
lblItemName.Size = new Size(61, 13);
lblItemName.TabIndex = 10;
lblItemName.Text = "Item Name:";
txtItemName.Location = new Point(95, 100);
txtItemName.Size = new Size(329, 20);
txtItemName.TabIndex = 11;
lblItemSize.AutoSize = true;
lblItemSize.Location = new Point(12, 132);
lblItemSize.Size = new Size(30, 13);
lblItemSize.TabIndex = 12;
lblItemSize.Text = "Size:";
txtItemSize.Location = new Point(95, 129);
txtItemSize.Size = new Size(96, 20);
txtItemSize.TabIndex = 13;
lblUnitPrice.AutoSize = true;
lblUnitPrice.Location = new Point(224, 135);
lblUnitPrice.Size = new Size(56, 13);
lblUnitPrice.TabIndex = 14;
lblUnitPrice.Text = "Unit Price:";
txtUnitPrice.Location = new Point(304, 132);
txtUnitPrice.Size = new Size(121, 20);
txtUnitPrice.TabIndex = 15;
txtUnitPrice.Text = "0.00";
txtUnitPrice.TextAlign = HorizontalAlignment.Right;
lblDiscountRate.AutoSize = true;
lblDiscountRate.Location = new Point(11, 161);
lblDiscountRate.Size = new Size(78, 13);
lblDiscountRate.TabIndex = 16;
lblDiscountRate.Text = "Discount Rate:";
txtDiscountRate.Location = new Point(95, 158);
txtDiscountRate.Size = new Size(96, 20);
txtDiscountRate.TabIndex = 17;
txtDiscountRate.Text = "0.00";
txtDiscountRate.TextAlign = HorizontalAlignment.Right;
lblPercent.AutoSize = true;
lblPercent.Font = new Font("Microsoft Sans Serif", 9.75F,
FontStyle.Bold,
GraphicsUnit.Point, ((byte)(0)));
lblPercent.Location = new Point(191, 161);
lblPercent.Size = new Size(21, 16);
lblPercent.TabIndex = 18;
lblPercent.Text = "%";
lblSaleStatus.AutoSize = true;
lblSaleStatus.Location = new Point(224, 164);
lblSaleStatus.Size = new Size(64, 13);
lblSaleStatus.TabIndex = 19;
lblSaleStatus.Text = "Sale Status:";
cbxSaleStatus.FormattingEnabled = true;
cbxSaleStatus.Items.AddRange(new object[] {
"Sold", "In Stock",
"On Display", "Processing", "Other"});
cbxSaleStatus.Location = new Point(304, 161);
cbxSaleStatus.Size = new Size(121, 21);
cbxSaleStatus.TabIndex = 20;
btnReset.Location = new Point(125, 193);
btnReset.Size = new Size(95, 23);
btnReset.TabIndex = 23;
btnReset.Text = "Reset";
btnReset.UseVisualStyleBackColor = true;
btnReset.Click += new System.EventHandler(btnResetClicked);
btnCreate.Location = new Point(226, 193);
btnCreate.Size = new Size(108, 23);
btnCreate.TabIndex = 21;
btnCreate.Text = "Create";
btnCreate.UseVisualStyleBackColor = true;
btnCreate.Click += new System.EventHandler(btnCreateClicked);
btnClose.Location = new Point(340, 193);
btnClose.Size = new Size(83, 23);
btnClose.TabIndex = 22;
btnClose.Text = "Close";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += new System.EventHandler(btnCloseClicked);
ClientSize = new Size(437, 230);
Controls.Add(txtManufacturer);
Controls.Add(lblPercent);
Controls.Add(txtDiscountRate);
Controls.Add(lblDiscountRate);
Controls.Add(txtArrivalDate);
Controls.Add(lblArrivalDate);
Controls.Add(btnClose);
Controls.Add(btnCreate);
Controls.Add(btnReset);
Controls.Add(txtUnitPrice);
Controls.Add(lblUnitPrice);
Controls.Add(cbxSaleStatus);
Controls.Add(lblSaleStatus);
Controls.Add(txtItemSize);
Controls.Add(lblItemSize);
Controls.Add(txtItemName);
Controls.Add(lblItemName);
Controls.Add(cbxSubCategories);
Controls.Add(lblSubCategory);
Controls.Add(cbxCategories);
Controls.Add(lblCategory);
Controls.Add(lblManufacturer);
Controls.Add(txtItemNumber);
Controls.Add(lblItemNumber);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Fun Department Store - Create New Store Item";
Load += new System.EventHandler(CreateStoreItemLoaded);
ResumeLayout(false);
PerformLayout();
}
private void btnResetClicked(object sender, EventArgs e)
{
Random rndNumber = new Random();
txtItemNumber.Text = rndNumber.Next(100000, 999999).ToString();
txtArrivalDate.Text = DateTime.Today.ToShortDateString();
txtManufacturer.Text = "";
cbxCategories.Text = "";
cbxSubCategories.Text = "";
txtItemName.Text = "";
txtItemSize.Text = "";
txtUnitPrice.Text = "0.00";
txtDiscountRate.Text = "";
cbxSaleStatus.Text = "";
}
private void CreateStoreItemLoaded(object sender, EventArgs e)
{
// When the form opens, reset it
btnResetClicked(sender, e);
}
private void btnCreateClicked(object sender, EventArgs e)
{
double unitPrice = 0.00;
double discountRate = 0.00;
FileStream fsStoreItems = null;
DateTime dtArrivalDate = DateTime.Today;
BinaryFormatter bfStoreItems = new BinaryFormatter();
List<StoreItem> lstStoreItems = new List<StoreItem>();
string strFileName = @"\\Expression\\Fun Department Store\\StoreItems.fds";
// Make sure the user entered an item number.
// If not, don't do anything
if (txtItemNumber.Text.Length == 0)
{
MessageBox.Show("You must provide a (unique) item number.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Make sure the user entered a name for the new item.
// If not, don't do anything
if (txtItemName.Text.Length == 0)
{
MessageBox.Show("You must provide a name for the item.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Make sure the user provided a price for the item
if (txtUnitPrice.Text.Length == 0)
{
MessageBox.Show("You must provide a price for the item.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
dtArrivalDate = DateTime.Parse(txtArrivalDate.Text);
}
catch (FormatException)
{
}
// Check if the user provided a valid price...
try
{
unitPrice = double.Parse(txtUnitPrice.Text);
}
catch (FormatException)
{
// ... if not, use 0.00
}
// Check if the user provided a valid discount rate...
try
{
discountRate = double.Parse(txtDiscountRate.Text);
}
catch (FormatException)
{
// ... if not, use 0.00
}
if (!Directory.Exists(@"\\Expression\\Fun Department Store"))
{
MessageBox.Show("The folder used to hold the records doesn't exist.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (File.Exists(strFileName))
{
fsStoreItems = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
lstStoreItems =
(List<StoreItem>)(bfStoreItems.Deserialize(fsStoreItems));
}
finally
{
fsStoreItems.Close();
}
}
StoreItem si = new StoreItem();
si.ItemNumber = txtItemNumber.Text;
if (txtArrivalDate.Text != "")
si.ArrivalDate = dtArrivalDate;
si.Manufacturer = txtManufacturer.Text;
si.Category = cbxCategories.Text;
si.SubCategory = cbxSubCategories.Text;
si.ItemName = txtItemName.Text;
si.ItemSize = txtItemSize.Text;
si.UnitPrice = unitPrice;
if (txtDiscountRate.Text != "")
si.DiscountRate = discountRate;
si.SaleStatus = cbxSaleStatus.Text;
lstStoreItems.Add(si);
fsStoreItems = new FileStream(strFileName, FileMode.Create,
FileAccess.Write, FileShare.Write);
try
{
bfStoreItems.Serialize(fsStoreItems, lstStoreItems);
// Once the item has been created, let the user know
MessageBox.Show("The item has been created",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
btnResetClicked(sender, e);
}
finally
{
fsStoreItems.Close();
}
}
private void btnCloseClicked(object sender, EventArgs e)
{
Close();
}
}
- In the Solution Explorer, double-click CreateStoreItem.cs
- Display the StoreInventory.cs file
- Change the ShowInventory() method as follows:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class StoreInventory : Form
{
private ColumnHeader colIndex;
private ColumnHeader colItemNumber;
private ColumnHeader colArrivalDate;
private ColumnHeader colManufacturer;
private ColumnHeader colCategory;
private ColumnHeader colSubCategory;
private ColumnHeader colItemName;
private ColumnHeader colItemSize;
private ColumnHeader colUnitPrice;
private ColumnHeader colSaleStatus;
private ListView lvwStoreItems;
private Button btnClose;
public StoreInventory()
{
InitializeComponent();
}
private void InitializeComponent()
{
colIndex = new System.Windows.Forms.ColumnHeader();
colItemNumber = new System.Windows.Forms.ColumnHeader();
colArrivalDate = new System.Windows.Forms.ColumnHeader();
colManufacturer = new System.Windows.Forms.ColumnHeader();
colCategory = new System.Windows.Forms.ColumnHeader();
colSubCategory = new System.Windows.Forms.ColumnHeader();
colItemName = new System.Windows.Forms.ColumnHeader();
colItemSize = new System.Windows.Forms.ColumnHeader();
colUnitPrice = new System.Windows.Forms.ColumnHeader();
colSaleStatus = new System.Windows.Forms.ColumnHeader();
lvwStoreItems = new System.Windows.Forms.ListView();
btnClose = new System.Windows.Forms.Button();
SuspendLayout();
colIndex.Text = "Index";
colIndex.Width = 40;
colItemNumber.Text = "Item #";
colItemNumber.TextAlign = HorizontalAlignment.Center;
colItemNumber.Width = 50;
colArrivalDate.Text = "Arrival Date";
colArrivalDate.Width = 70;
colManufacturer.Text = "Manufacturer";
colManufacturer.Width = 100;
colCategory.Text = "Category";
colSubCategory.Text = "Sub-Category";
colSubCategory.Width = 80;
colItemName.Text = "Item Name/Description";
colItemName.Width = 220;
colItemSize.Text = "Size";
colItemSize.TextAlign = HorizontalAlignment.Center;
colUnitPrice.Text = "Unit Price";
colUnitPrice.TextAlign = HorizontalAlignment.Right;
colSaleStatus.Text = "Status";
colSaleStatus.Width = 70;
btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnClose.Location = new Point(772, 182);
btnClose.Size = new Size(75, 23);
btnClose.TabIndex = 33;
btnClose.Text = "Close";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += new System.EventHandler(btnCloseClicked);
lvwStoreItems.Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
AnchorStyles.Left | AnchorStyles.Right;
lvwStoreItems.Columns.AddRange(new ColumnHeader[] {
colIndex, colItemNumber, colArrivalDate, colManufacturer,
colCategory, colSubCategory, colItemName,
colItemSize, colUnitPrice, colSaleStatus});
lvwStoreItems.FullRowSelect = true;
lvwStoreItems.GridLines = true;
lvwStoreItems.Location = new Point(12, 12);
lvwStoreItems.Size = new Size(835, 160);
lvwStoreItems.TabIndex = 32;
lvwStoreItems.UseCompatibleStateImageBehavior = false;
lvwStoreItems.View = System.Windows.Forms.View.Details;
ClientSize = new Size(859, 213);
Controls.Add(btnClose);
Controls.Add(lvwStoreItems);
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Fun Department Store - Current Store Inventory";
Load += new System.EventHandler(StoreInventoryLoaded);
ResumeLayout(false);
}
private void ShowInventory()
{
List StoreItems;
FileStream fsStoreItems = null;
BinaryFormatter bfStoreItems = new BinaryFormatter();
// If there were some items in the list view, remove them before filling it up
lvwStoreItems.Items.Clear();
string strFileName = @"\\Expression\\Fun Department Store\\StoreItems.fds";
if (File.Exists(strFileName))
{
fsStoreItems = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
StoreItems = (List)(bfStoreItems.Deserialize(fsStoreItems));
// Using the total number of records, display each in the list view
for(int i = 0; i < StoreItems.Count; i++)
{
StoreItem si = StoreItems[i];
ListViewItem lviStoreItem = new ListViewItem((i + 1).ToString());
lviStoreItem.SubItems.Add(si.ItemNumber);
lviStoreItem.SubItems.Add(si.ArrivalDate.ToShortDateString());
lviStoreItem.SubItems.Add(si.Manufacturer);
lviStoreItem.SubItems.Add(si.Category);
lviStoreItem.SubItems.Add(si.SubCategory);
lviStoreItem.SubItems.Add(si.ItemName);
lviStoreItem.SubItems.Add(si.ItemSize);
lviStoreItem.SubItems.Add(si.DiscountRate.ToString("F"));
lviStoreItem.SubItems.Add(si.UnitPrice.ToString("F"));
lviStoreItem.SubItems.Add(si.SaleStatus);
lvwStoreItems.Items.Add(lviStoreItem);
}
}
finally
{
fsStoreItems.Close();
}
}
}
private void StoreInventoryLoaded(object sender, EventArgs e)
{
ShowInventory();
}
private void btnCloseClicked(object sender, EventArgs e)
{
Close();
}
}
- To create a new file, on the main menu, click Project -> Add New Item...
-
In the middle list, make sure Code File is selected.
Change the Name to
Employees - Click Add
- In the empty document, type the
following:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class Employees : Form
{
private ColumnHeader colEmployeeNumber;
private ColumnHeader colFirstName;
private ColumnHeader colLastName;
private ColumnHeader colTitle;
private ColumnHeader colManager;
private ColumnHeader colHourlySalary;
private ColumnHeader colUsername;
private ColumnHeader colPassword;
private ListView lvwEmployees;
public GroupBox grpNewEmployee;
private Label lblEmployeeNumber;
private MaskedTextBox txtEmployeeNumber;
private Label lblFirstName;
private TextBox txtFirstName;
private Label lblLastName;
private TextBox txtLastName;
private Label lblTitle;
private TextBox txtTitle;
private Button btnReset;
private CheckBox chkManager;
private Label lblHourlySalary;
private TextBox txtHourlySalary;
private Button btnSubmit;
private Label lblUsername;
private TextBox txtUsername;
private Label lblPassword;
private TextBox txtPassword;
private Button btnClose;
public Employees()
{
InitializeComponent();
}
private void InitializeComponent()
{
colEmployeeNumber = new ColumnHeader();
colFirstName = new ColumnHeader();
colLastName = new ColumnHeader();
colTitle = new ColumnHeader();
colManager = new ColumnHeader();
colHourlySalary = new ColumnHeader();
colUsername = new ColumnHeader();
colPassword = new ColumnHeader();
lvwEmployees = new ListView();
grpNewEmployee = new GroupBox();
lblEmployeeNumber = new Label();
txtEmployeeNumber = new MaskedTextBox();
lblFirstName = new Label();
txtFirstName = new TextBox();
txtLastName = new TextBox();
lblLastName = new Label();
lblTitle = new Label();
txtTitle = new TextBox();
btnReset = new Button();
chkManager = new CheckBox();
lblHourlySalary = new Label();
txtHourlySalary = new TextBox();
btnSubmit = new Button();
lblUsername = new Label();
txtUsername = new TextBox();
lblPassword = new Label();
txtPassword = new TextBox();
btnClose = new Button();
grpNewEmployee.SuspendLayout();
SuspendLayout();
colEmployeeNumber.Text = "Employee #";
colEmployeeNumber.Width = 70;
colFirstName.Text = "First Name";
colFirstName.Width = 80;
colLastName.Text = "Last Name";
colLastName.Width = 80;
colTitle.Text = "Title";
colTitle.Width = 120;
colManager.Text = "Manager";
colHourlySalary.Text = "Salary";
colHourlySalary.TextAlign = HorizontalAlignment.Right;
colHourlySalary.Width = 50;
colUsername.Text = "Username";
colPassword.Text = "Password";
lvwEmployees.Columns.AddRange(new ColumnHeader[] {
colEmployeeNumber, colFirstName, colLastName, colTitle,
colManager, colHourlySalary, colUsername, colPassword});
lvwEmployees.FullRowSelect = true;
lvwEmployees.GridLines = true;
lvwEmployees.Location = new Point(12, 12);
lvwEmployees.Size = new Size(606, 191);
lvwEmployees.TabIndex = 0;
lvwEmployees.UseCompatibleStateImageBehavior = false;
lvwEmployees.View = System.Windows.Forms.View.Details;
lblEmployeeNumber.AutoSize = true;
lblEmployeeNumber.Location = new Point(42, 29);
lblEmployeeNumber.Size = new Size(66, 13);
lblEmployeeNumber.TabIndex = 0;
lblEmployeeNumber.Text = "Employee #:";
txtEmployeeNumber.Location = new Point(124, 26);
txtEmployeeNumber.Mask = "00-000";
txtEmployeeNumber.Size = new Size(52, 20);
txtEmployeeNumber.TabIndex = 1;
lblFirstName.AutoSize = true;
lblFirstName.Location = new Point(42, 55);
lblFirstName.Size = new Size(60, 13);
lblFirstName.TabIndex = 2;
lblFirstName.Text = "First Name:";
txtFirstName.Location = new Point(124, 52);
txtFirstName.Size = new Size(100, 20);
txtFirstName.TabIndex = 3;
txtLastName.Location = new Point(327, 52);
txtLastName.Size = new Size(100, 20);
txtLastName.TabIndex = 5;
lblLastName.AutoSize = true;
lblLastName.Location = new Point(245, 55);
lblLastName.Size = new Size(61, 13);
lblLastName.TabIndex = 4;
lblLastName.Text = "Last Name:";
lblTitle.AutoSize = true;
lblTitle.Location = new Point(42, 81);
lblTitle.Size = new Size(30, 13);
lblTitle.TabIndex = 6;
lblTitle.Text = "Title:";
txtTitle.Location = new Point(124, 78);
txtTitle.Size = new Size(303, 20);
txtTitle.TabIndex = 7;
btnReset.Location = new Point(447, 104);
btnReset.Size = new Size(97, 23);
btnReset.TabIndex = 15;
btnReset.Text = "Reset";
btnReset.UseVisualStyleBackColor = true;
btnReset.Click += new System.EventHandler(btnResetClicked);
chkManager.AutoSize = true;
chkManager.CheckAlign = ContentAlignment.MiddleRight;
chkManager.Location = new Point(45, 107);
chkManager.Size = new Size(91, 17);
chkManager.TabIndex = 8;
chkManager.Text = "Is a Manager:";
chkManager.UseVisualStyleBackColor = true;
txtHourlySalary.Location = new Point(327, 104);
txtHourlySalary.Size = new Size(100, 20);
txtHourlySalary.TabIndex = 10;
txtHourlySalary.Text = "0.00";
txtHourlySalary.TextAlign = HorizontalAlignment.Right;
lblHourlySalary.AutoSize = true;
lblHourlySalary.Location = new Point(245, 107);
lblHourlySalary.Size = new Size(72, 13);
lblHourlySalary.TabIndex = 9;
lblHourlySalary.Text = "Hourly Salary:";
btnSubmit.Location = new Point(447, 132);
btnSubmit.Size = new Size(97, 23);
btnSubmit.TabIndex = 16;
btnSubmit.Text = "Submit";
btnSubmit.UseVisualStyleBackColor = true;
btnSubmit.Click += new System.EventHandler(btnSubmitClicked);
lblUsername.AutoSize = true;
lblUsername.Location = new Point(42, 133);
lblUsername.Size = new Size(58, 13);
lblUsername.TabIndex = 11;
lblUsername.Text = "Username:";
txtUsername.Location = new Point(124, 130);
txtUsername.Size = new Size(100, 20);
txtUsername.TabIndex = 12;
lblPassword.AutoSize = true;
lblPassword.Location = new Point(245, 132);
lblPassword.Size = new Size(56, 13);
lblPassword.TabIndex = 13;
lblPassword.Text = "Password:";
txtPassword.Location = new Point(327, 130);
txtPassword.PasswordChar = '*';
txtPassword.Size = new Size(100, 20);
txtPassword.TabIndex = 14;
btnClose.Location = new Point(459, 384);
btnClose.Size = new Size(97, 23);
btnClose.TabIndex = 17;
btnClose.Text = "Close";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += new System.EventHandler(btnCloseClicked);
grpNewEmployee.Controls.Add(lblEmployeeNumber);
grpNewEmployee.Controls.Add(txtEmployeeNumber);
grpNewEmployee.Controls.Add(lblFirstName);
grpNewEmployee.Controls.Add(txtFirstName);
grpNewEmployee.Controls.Add(lblLastName);
grpNewEmployee.Controls.Add(txtLastName);
grpNewEmployee.Controls.Add(lblTitle);
grpNewEmployee.Controls.Add(txtTitle);
grpNewEmployee.Controls.Add(btnReset);
grpNewEmployee.Controls.Add(chkManager);
grpNewEmployee.Controls.Add(lblHourlySalary);
grpNewEmployee.Controls.Add(txtHourlySalary);
grpNewEmployee.Controls.Add(btnSubmit);
grpNewEmployee.Controls.Add(lblUsername);
grpNewEmployee.Controls.Add(txtUsername);
grpNewEmployee.Controls.Add(lblPassword);
grpNewEmployee.Controls.Add(txtPassword);
grpNewEmployee.Location = new Point(12, 209);
grpNewEmployee.Size = new Size(605, 165);
grpNewEmployee.TabIndex = 1;
grpNewEmployee.TabStop = false;
grpNewEmployee.Text = "New Employee";
ClientSize = new Size(629, 414);
Controls.Add(grpNewEmployee);
Controls.Add(lvwEmployees);
Controls.Add(btnClose);
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
Text = "Fun Department Store - Employees";
Load += new System.EventHandler(Employees_Load);
grpNewEmployee.ResumeLayout(false);
grpNewEmployee.PerformLayout();
ResumeLayout(false);
}
private void ShowEmployees()
{
FileStream fsEmployees = null;
List<Employee> lstEmployees = new List<Employee>();
BinaryFormatter bfEmployees = new BinaryFormatter();
// If there were some items in the list view, remove them before filling it up
lvwEmployees.Items.Clear();
string strFileName = @"\\Expression\\Fun Department Store\\Employees.fds";
if (File.Exists(strFileName))
{
fsEmployees = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
lstEmployees = (List<Employee>)(bfEmployees.Deserialize(fsEmployees));
// Using the total number of records, display each in the list view
foreach (Employee empl in lstEmployees)
{
ListViewItem lviEmployee = new ListViewItem(empl.EmployeeNumber);
lviEmployee.SubItems.Add(empl.FirstName);
lviEmployee.SubItems.Add(empl.LastName);
lviEmployee.SubItems.Add(empl.Title);
lviEmployee.SubItems.Add(empl.Manager.ToString());
lviEmployee.SubItems.Add(empl.HourlySalary.ToString("F"));
lviEmployee.SubItems.Add(empl.Username);
lviEmployee.SubItems.Add(empl.Password);
lvwEmployees.Items.Add(lviEmployee);
}
}
finally
{
fsEmployees.Close();
}
}
}
private void Employees_Load(object sender, EventArgs e)
{
ShowEmployees();
btnResetClicked(sender, e);
}
private void btnResetClicked(object sender, EventArgs e)
{
Random rndNumber = new Random();
txtEmployeeNumber.Text = rndNumber.Next(10000, 99999).ToString();
txtFirstName.Text = "";
txtLastName.Text = "";
txtTitle.Text = "";
chkManager.Checked = false;
txtHourlySalary.Text = "0.00";
txtUsername.Text = "";
txtPassword.Text = "";
}
private void btnSubmitClicked(object sender, EventArgs e)
{
double hourlySalary = 0.00;
FileStream fsEmployees = null;
DateTime dtArrivalDate = DateTime.Today;
List<Employee> lstEmployees = new List<Employee>();
BinaryFormatter bfEmployees = new BinaryFormatter();
string strFileName = @"\\Expression\\Fun Department Store\\Employees.fds";
// Make sure the user entered an employeee number.
// If not, don't do anything
if (txtEmployeeNumber.Text.Length == 0)
{
MessageBox.Show("You must provide a (unique) employee number.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Make sure the user entered a last name for the new employee.
// If not, don't do anything
if (txtLastName.Text.Length == 0)
{
MessageBox.Show("You must provide a last name for the employee.",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Check if the user provided a valid hourly salary...
try
{
hourlySalary = double.Parse(txtHourlySalary.Text);
}
catch (FormatException)
{
// ... if not, use 0.00
}
if (File.Exists(strFileName))
{
fsEmployees = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
lstEmployees = (List<Employee>)(bfEmployees.Deserialize(fsEmployees));
}
finally
{
fsEmployees.Close();
}
}
Employee empl = new Employee();
empl.EmployeeNumber = txtEmployeeNumber.Text;
empl.FirstName = txtFirstName.Text;
empl.LastName = txtLastName.Text;
empl.Title = txtTitle.Text;
empl.Manager = chkManager.Checked;
if (txtHourlySalary.Text != "")
empl.HourlySalary = hourlySalary;
empl.Username = txtUsername.Text;
empl.Password = txtPassword.Text;
lstEmployees.Add(empl);
try
{
fsEmployees = new FileStream(strFileName, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.Write);
bfEmployees.Serialize(fsEmployees, lstEmployees);
// Once the employee record has been created, let the user know
MessageBox.Show("The employee's record has been created",
"Fun Department Store",
MessageBoxButtons.OK, MessageBoxIcon.Information);
btnResetClicked(sender, e);
}
finally
{
fsEmployees.Close();
}
// If there were some items in the list view, remove them before filling it up
lvwEmployees.Items.Clear();
if (File.Exists(strFileName))
{
fsEmployees = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
lstEmployees = (List<Employee>)(bfEmployees.Deserialize(fsEmployees));
// Using the total number of records, display each in the list view
foreach (Employee empl1 in lstEmployees)
{
ListViewItem lviEmployee = new ListViewItem(empl1.EmployeeNumber);
lviEmployee.SubItems.Add(empl1.FirstName);
lviEmployee.SubItems.Add(empl1.LastName);
lviEmployee.SubItems.Add(empl1.Title);
lviEmployee.SubItems.Add(empl1.Manager.ToString());
lviEmployee.SubItems.Add(empl1.HourlySalary.ToString("F"));
lviEmployee.SubItems.Add(empl1.Username);
lviEmployee.SubItems.Add(empl1.Password);
lvwEmployees.Items.Add(lviEmployee);
}
}
finally
{
fsEmployees.Close();
}
}
}
private void btnCloseClicked(object sender, EventArgs e)
{
Close();
}
}
- In the Solution Explorer, double-click the Employees.cs
- To execute, press F5
- Click Employees
- Create a few records (let
the application generate employees numbers)
- Close the employees table
- Click Create Store Item
- Create a few records
(let the application generate the items numbers)
- Close the Create Store Item form
- Click the Store Inventory button
- Close the
- Store Inventory form
- Close the Switchboard form
Introduction to Implementing Authentication |
|
To implement our authentication, we will create a C#
application. Normally, you can use Microsoft Visual C# 2010 Express but we will
use Microsoft Visual Studio 2010 Professional. To establish a connection to the
Microsoft Access database, we will use the .NET Framework.
As always, you can first create the application and then
take care of authentication later.
|
|