public void saveProduct(Product Pro) { cmd.CommandText = "INSERT INTO product(name, quantity, price)" + " values(@name, @quantity, @price)"; cmd.Parameters.AddWithValue("@name", Pro.getName()); cmd.Parameters.AddWithValue("@quantity", Pro.getQuantity()); cmd.Parameters.AddWithValue("@price", Pro.getPrice()); cmd.ExecuteNonQuery(); conn.Close(); }
public void updateProduct(Product Pro) { cmd.CommandText = " UPDATE product SET name = @name, quantity = @quantity, " + " price = @price " + " where id = @id "; cmd.Parameters.AddWithValue("@name", Pro.getName()); cmd.Parameters.AddWithValue("@quantity", Pro.getQuantity()); cmd.Parameters.AddWithValue("@price", Pro.getPrice()); cmd.Parameters.AddWithValue("@id", Pro.getId()); cmd.ExecuteNonQuery(); conn.Close(); }
private void button1_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to add a product?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { name = textBoxName.Text; quantity = double.Parse(textBoxQuantity.Text); price = double.Parse(textBoxPrice.Text); pro = new Product(name, quantity, price); if (pro.repOK()) { proMan = new ProductManager(); proMan.saveProduct(pro); //write text file //MessageBox.Show(cus.toString()); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Add Product{" + pro.toString() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Add Product successfully."); } else { MessageBox.Show("Invalid Product."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button3_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to delete a product?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); proMan = new ProductManager(); proMan.deleteProduct(id); //write text file pro = new Product(); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Delete Product{" + id + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Delete Product successfully."); } else { MessageBox.Show("Select a product to delete."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }