public bool update(MenuItm itm) { bool result = false; con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString); try { cmd = new SqlCommand( "UPDATE menu_items SET item_name=@name, item_price=@price, item_category=@category WHERE item_id=@id", con); cmd.Parameters.AddWithValue("@id", itm.item_id); cmd.Parameters.AddWithValue("@name", itm.item_name); cmd.Parameters.AddWithValue("@price", itm.item_price.ToString()); cmd.Parameters.AddWithValue("@category", itm.item_category); con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { result = true; } else { MessageBox.Show("Failed to Update. Try Again !", "Error"); } } catch (Exception e) { if (e.ToString().Contains("Violation of UNIQUE KEY")) { MessageBox.Show("Item name already exist !", "Error"); } else { MessageBox.Show(e.ToString(), "Error"); } } finally { con.Close(); } return(result); }
public bool delete(MenuItm itm) { bool del_result = false; bool update_result = false; con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString); try { con.Open(); cmd = new SqlCommand("SELECT * FROM menu_items WHERE item_id=@Id", con); cmd.Parameters.AddWithValue("@Id", itm.item_id); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { itm.item_name = reader["item_name"].ToString(); itm.item_price = float.Parse(reader["item_price"].ToString()); itm.item_category = reader["item_category"].ToString(); itm.item_dec = reader["description"].ToString(); con.Close(); con.Open(); cmd = new SqlCommand( "INSERT INTO deleted_menu_items (del_item_id, del_item_name, del_item_price, del_item_category, del_item_description) VALUES(@id,@name,@price,@cate,@desc); ", con); cmd.Parameters.AddWithValue("@id", itm.item_id); cmd.Parameters.AddWithValue("@name", itm.item_name); cmd.Parameters.AddWithValue("@price", itm.item_price.ToString()); cmd.Parameters.AddWithValue("@cate", itm.item_category); cmd.Parameters.AddWithValue("@desc", itm.item_dec); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { update_result = true; } else { update_result = false; } } } catch (Exception exc) { con.Close(); try { cmd = new SqlCommand( "DELETE FROM deleted_menu_items WHERE del_item_id=@id", con); cmd.Parameters.AddWithValue("@id", itm.item_id); con.Open(); cmd.ExecuteNonQuery(); } catch (Exception exce) { MessageBox.Show(exce.ToString(), "Error"); } finally { MessageBox.Show("Failed to Delete. Try Again !", "Error"); } } finally { con.Close(); } try { con.Open(); if (update_result) { cmd = new SqlCommand( "DELETE FROM menu_items WHERE item_id=@id", con); cmd.Parameters.AddWithValue("@id", itm.item_id); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { del_result = true; } else { del_result = false; MessageBox.Show("Failed to Delete. Try Again !", "Error"); } } } catch (Exception e) { MessageBox.Show("Failed to Delete. Try Again !", "Error"); } finally { con.Close(); } return(del_result); }