Sei sulla pagina 1di 9

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.

Text; using System.Windows.Forms; using DBConnection; using System.Data.OleDb; namespace OrderingProgram { public partial class ADD_SUPPLIER : Form { Connection cn; OleDbCommand cmd = new OleDbCommand(); OleDbDataReader dr; public ADD_SUPPLIER() { InitializeComponent(); } private void ADD_SUPPLIER_Load(object sender, EventArgs e) { cn = new Connection(Application.StartupPath.ToString(), "POS.mdb"); FillSupplier(); } private string GenID(string tblName, string fieldName) { string retVal; while (true) { retVal = Randomize(); if (!retVal.Equals(DataLookUp(tblName, fieldName))) break; } return retVal; } private string Randomize() { string retVal = ""; Random rnd = new Random(); for (int i = 0; i < 5; i++) { retVal = retVal + Convert.ToChar(rnd.Next(65, 91)); } return retVal; } private string DataLookUp(string tblName, string fieldName) { OleDbCommand cmdLookUp = new OleDbCommand(); string retVal = ""; string sql = "SELECT " + fieldName + " FROM " + tblName + " "; cmdLookUp.CommandText = sql; cmdLookUp.Connection = cn.ConnectionStr(); dr = cmdLookUp.ExecuteReader(); if (dr.HasRows) {

while (dr.Read()) retVal = dr[0].ToString(); } else return null; return retVal; } private void saveSupplier() { if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !textBox3.Text.Equals("") && !textBox4.Text.Equals("")) { string sql = "INSERT INTO tblSupplier (SupplierID,SupplierName,CompleteAddress, ContactNumber, FaxNumber)" + "VALUES('" + GenID("tblSupplier", "SupplierID") + "','" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "', '" + textBox4.Text + "' )"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); cmd.ExecuteNonQuery(); FillSupplier(); } } private void ifSupplierExist() { if (!listView1.Items.Count.Equals(0)) { for (int i = 0; i < listView1.Items.Count; i++) { if (textBox1.Text.Equals(listView1.Items[i].Text)) { MessageBox.Show("Supplier Already Exist"); textBox1.Clear(); } } saveSupplier(); } } private void FillSupplier() { listView1.Items.Clear(); string sql = "SELECT SupplierName FROM tblSupplier;"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem list = new ListViewItem(dr[0].ToString()); listView1.Items.AddRange(new ListViewItem[] { list }); } dr.Close(); } private void button1_Click(object sender, EventArgs e) { if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !textBox3.Text.Equals("") && !textBox4.Text.Equals(""))

{ DialogResult dlgRes = MessageBox.Show("Are you sure you want to add this?", "Add New Supplier", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgRes.Equals(DialogResult.Yes)) { ifSupplierExist(); } } else { MessageBox.Show("Please fill out all the required fields."); } } private void textBox3_TextChanged(object sender, EventArgs e) { uint x = 0; if (!UInt32.TryParse(textBox3.Text, out x)) { MessageBox.Show("Invalid input, numbers only."); textBox3.Text = "0"; } } private void textBox4_TextChanged(object sender, EventArgs e) { uint x = 0; if (!UInt32.TryParse(textBox3.Text, out x)) { MessageBox.Show("Invalid input, numbers only."); textBox3.Text = "0"; } } } } namespace OrderingProgram { public partial class ADD_VARIANTS : Form { Connection cn; OleDbCommand cmd = new OleDbCommand(); OleDbDataReader dr; public ADD_VARIANTS() { InitializeComponent(); } private void ADD_VARIANTS_Load(object sender, EventArgs e) { cn = new Connection(Application.StartupPath.ToString(), "POS.mdb"); FillVariants(); } private string GenID(string tblName, string fieldName) { string retVal;

while (true) { retVal = ProduceRandomChars(); if (!retVal.Equals(DataLookUp(tblName, fieldName))) break; } return retVal; } private string ProduceRandomChars() { string retVal = ""; Random rnd = new Random(); for (int i = 0; i < 5; i++) { retVal = retVal + Convert.ToChar(rnd.Next(65, 91)); } return retVal; } private string DataLookUp(string tblName, string fieldName) { OleDbCommand cmdLookUp = new OleDbCommand(); string retVal = ""; string sql = "SELECT " + fieldName + " FROM " + tblName + " "; cmdLookUp.CommandText = sql; cmdLookUp.Connection = cn.ConnectionStr(); dr = cmdLookUp.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) retVal = dr[0].ToString(); } else return null; return retVal; } private void SaveVariant() { if (!textBox1.Text.Equals("")) { OleDbCommand cmdSave = new OleDbCommand(); string sql = "INSERT INTO tblVariants (VariantCode,VariantName) " + "VALUES('" + GenID("tblVariants", "VariantCode") + "','" + textBox1.Text + "')"; cmdSave.CommandText = sql; cmdSave.Connection = cn.ConnectionStr(); cmdSave.ExecuteNonQuery(); FillVariants(); } } private void FillVariants() { listView1.Items.Clear(); string sql = "SELECT VariantName,VariantCode From tblVariants;"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); dr = cmd.ExecuteReader();

while (dr.Read()) { ListViewItem list = new ListViewItem(dr[0].ToString()); list.SubItems.Add(dr[1].ToString()); listView1.Items.AddRange(new ListViewItem[] { list }); } dr.Close(); } private void ifVariantExist() { if ((!listView1.Items.Count.Equals(0)) || (!textBox1.Text.Length.Equals(0))) { for (int i = 0; i < listView1.Items.Count; i++) { if (textBox1.Text.Equals(listView1.Items[i].Text)) { DialogResult dlgRes = MessageBox.Show("Variant Name Already Exist", "INVALID INPUT", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); textBox1.Clear(); } } SaveVariant(); } } private void button1_Click(object sender, EventArgs e) { if (!textBox1.Text.Length.Equals(0)) { DialogResult dlgRes = MessageBox.Show("Are you sure you want to add variant?", "ADD NEW VARIANT", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgRes.Equals(DialogResult.Yes)) { ifVariantExist(); } } } } } _____________________________________________________________________________ namespace OrderingProgram { public partial class AddProduct : Form { Connection cn; OleDbCommand cmd = new OleDbCommand(); OleDbDataReader dr; public AddProduct() { InitializeComponent(); } private void AddProduct_Load(object sender, EventArgs e) { cn = new Connection(Application.StartupPath.ToString(), "POS.mdb");

FillProducts(); FillStocks(); } private void FillProducts() { listView1.Items.Clear(); string sql = "SELECT ProductName, VariantCode,SupplierCode FROM tblProducts"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem list = new ListViewItem(dr[0].ToString()); list.SubItems.Add(dr[1].ToString()); list.SubItems.Add(dr[2].ToString()); listView1.Items.AddRange(new ListViewItem[] { list }); } dr.Close(); } private string GenID(string tblName, string fieldName) { string retVal; while (true) { retVal = ProduceRandomChars(); if (!retVal.Equals(DataLookUp(tblName, fieldName))) break; } return retVal; } private string ProduceRandomChars() { string retVal = ""; Random rnd = new Random(); for (int i = 0; i < 10; i++) { retVal = retVal + Convert.ToChar(rnd.Next(65, 91)); } return retVal; } private string DataLookUp(string tblName, string fieldName) { OleDbCommand cmdLookUp = new OleDbCommand(); string retVal = ""; string sql = "SELECT " + fieldName + " FROM " + tblName + " "; cmdLookUp.CommandText = sql; cmdLookUp.Connection = cn.ConnectionStr(); dr = cmdLookUp.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) retVal = dr[0].ToString(); } else return null;

return retVal; } private void SaveProduct() { OleDbCommand cmdSave = new OleDbCommand(); string sql = "INSERT INTO tblProducts(ProductCode,ProductName, VariantCode, Price, SupplierCode,DateSupplied) " + "VALUES('" + GenID("tblProducts", "ProductCode") + "','" + textBox1.Text + "'," + "'" + textBox2.Text + "','" + textBox3.Text + "'," + "'" + textBox4.Text + "','" + DateTime.Now + "')"; cmdSave.CommandText = sql; cmdSave.Connection = cn.ConnectionStr(); cmdSave.ExecuteNonQuery(); FillProducts(); } private void ifProductExist() { if (!listView1.Items.Count.Equals(0)) { for (int i = 0; i < listView1.Items.Count; i++) { if (textBox1.Text.Equals(listView1.Items[i].Text)) { MessageBox.Show("Product Name Already Exist"); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } } SaveProduct(); } } private void button1_Click(object sender, EventArgs e) { if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !textBox3.Text.Equals("") && !textBox4.Text.Equals("")) { DialogResult dlgRes = MessageBox.Show("Are you sure you want to add this?", "Add New Supplier", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgRes.Equals(DialogResult.Yes)) { ifProductExist(); } else { textBox1.Focus(); } } else {

MessageBox.Show("Please fill out all the required fields ."); } } private void AddStocks() { string sql = "UPDATE tblStocks SET Stock = Stock + " + Convert.ToInt32(textBox6.Text) + " WHERE tblStocks.ProductID = '" + textBox5.Text + "'"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); cmd.ExecuteNonQuery(); FillStocks(); } private void FillStocks() { listView2.Items.Clear(); string sql = "SELECT tblProducts.ProductCode, tblProducts.ProductName, tblStocks.Stock FROM tblProducts, tblStocks WHERE tblStocks.ProductID = tblProducts.ProductCode;"; cmd.CommandText = sql; cmd.Connection = cn.ConnectionStr(); dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem list = new ListViewItem(dr[0].ToString()); list.SubItems.Add(dr[1].ToString()); list.SubItems.Add(dr[2].ToString()); listView2.Items.AddRange(new ListViewItem[] { list }); } dr.Close(); } private void ifProductExistinListView2() { if (!listView2.Items.Count.Equals(0)) { for (int i = 0; i < listView1.Items.Count; i++) { if (!textBox1.Text.Equals(listView2.Items[i].Text)) { } } AddStocks(); } } private void button2_Click(object sender, EventArgs e) { if (!textBox5.Text.Equals("") && !textBox6.Text.Equals("")) { DialogResult dlgRes = MessageBox.Show("Are you sure you want to add this stock?", "ADD STOCKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgRes.Equals(DialogResult.Yes)) { ifProductExistinListView2();

} } } private void textBox6_TextChanged(object sender, EventArgs e) { try { int x = 0; bool y = Int32.TryParse(textBox6.Text, out x); if (x < 1) { MessageBox.Show("Invalid number of Stocks."); textBox6.Text = "0"; } } catch (Exception ex) { MessageBox.Show("Invalid number of Stocks."); textBox6.Text = "0"; } } private void button1_Click_1(object sender, EventArgs e) { if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !textBox3.Text.Equals("") && !textBox4.Text.Equals("")) { DialogResult dlgRes = MessageBox.Show("Are you sure you want to add this stock?", "ADD STOCKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgRes.Equals(DialogResult.Yes)) { ifProductExist(); } } } private void ifVariantCodeExistinListView1() { if (!listView2.Items.Count.Equals(0)) { for (int i = 0; i < listView1.Items.Count; i++) { if (!textBox1.Text.Equals(listView2.Items[i].SubItems[2])) { } } AddStocks(); } } private void listView2_Click(object sender, EventArgs e) { textBox5.Text = listView2.SelectedItems[0].Text; }

Potrebbero piacerti anche