public void saveIngredient(Ingredient Ing) { cmd.CommandText = "INSERT INTO ingredient(name, quantity, price, status)" + " values(@name, @quantity, @price, @status)"; cmd.Parameters.AddWithValue("@name", Ing.getName()); cmd.Parameters.AddWithValue("@quantity", Ing.getQuantity()); cmd.Parameters.AddWithValue("@price", Ing.getPrice()); cmd.Parameters.AddWithValue("@status", Ing.getStatus()); cmd.ExecuteNonQuery(); conn.Close(); }
public void updateIngredient(Ingredient Ing) { cmd.CommandText = " UPDATE ingredient SET name = @name, quantity = @quantity, " + " price= @price , status = @status " + " where id = @id "; cmd.Parameters.AddWithValue("@name", Ing.getName()); cmd.Parameters.AddWithValue("@quantity", Ing.getQuantity()); cmd.Parameters.AddWithValue("@price", Ing.getPrice()); cmd.Parameters.AddWithValue("@status", Ing.getStatus()); cmd.Parameters.AddWithValue("@id", Ing.getId()); cmd.ExecuteNonQuery(); conn.Close(); }
private void button1_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to add an ingredient?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { name = textBoxName.Text; quantity = double.Parse(textBoxQuantity.Text); price = double.Parse(textBoxPrice.Text); status = textBoxStatus.Text; Ing = new Ingredient(name, quantity, price, status); if (Ing.repOK() && Ing.validatePrice(price)) { IngMan = new IngredientManager(); IngMan.saveIngredient(Ing); //write text file //MessageBox.Show(cus.toString()); DateTime now = DateTime.Now; date = now.ToString("yyyy-MM-dd"); System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Add Ingredient{" + Ing.toString() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Add Ingredient successfully."); } else { MessageBox.Show("Invalid Ingredient."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button3_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to add an ingredient?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); IngMan = new IngredientManager(); IngMan.deleteIngredient(id); //write text file Ing = new Ingredient(); DateTime now = DateTime.Now; date = now.ToString("yyyy-MM-dd"); System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Delete Ingredient{" + id + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Delete Ingredient successfully."); } else { MessageBox.Show("Select an ingredient to delete."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button2_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to update an ingredient?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); name = textBoxName.Text; quantity = double.Parse(textBoxQuantity.Text); price = double.Parse(textBoxPrice.Text); status = textBoxStatus.Text; Ing = new Ingredient(id, name, quantity, price, status); if (Ing.repOK() && Ing.validatePrice(price)) { IngMan = new IngredientManager(); IngMan.updateIngredient(Ing); //write text file DateTime now = DateTime.Now; date = now.ToString("yyyy-MM-dd"); System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Update Ingredient{" + Ing.toString() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Update Ingredient successfully."); } else { MessageBox.Show("Invalid Ingredient."); } } else { MessageBox.Show("Select an ingredient to update."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }