//Update product private void button3_Click(object sender, EventArgs e) { if (textBox2.Text.Length == 0 || textBox3.Text.Length == 0 || comboBox1.Text.Length == 0) { MessageBox.Show("invalid enter !"); } else { products p = new products(); context c = new context(new check_product()); if (c.my_function(textBox2.Text) && int.Parse(textBox3.Text) != 0) { if (comboBox1.Text == "NOT CHANGE") { p.update_product(textBox2.Text, int.Parse(textBox3.Text), 0); MessageBox.Show("Done"); } else { p.update_product(textBox2.Text, int.Parse(textBox3.Text), int.Parse(comboBox1.Text)); MessageBox.Show("Done"); } } else { } } }
public string return_product(int bill_id, string product, int quantity) { string return_message; if (File.Exists("bills.txt")) { FileStream fs = new FileStream("bills.txt", FileMode.Open); BinaryFormatter f = new BinaryFormatter(); Dictionary <int, orders> list = new Dictionary <int, orders>(); KeyValuePair <string, int> li = new KeyValuePair <string, int>(); // bool falg = false; DateTime n = DateTime.Now; while (fs.Position < fs.Length) { orders o = (orders)f.Deserialize(fs); list[o.id] = o; } fs.Close(); if (list.ContainsKey(bill_id)) { if (list[bill_id].date.Day - n.Day <= 14) { for (int i = 0; i < list[bill_id].products.Count; i++) { if (list[bill_id].products.ElementAt(i).Key == product) { if (list[bill_id].products.ElementAt(i).Value >= quantity && quantity != 0) { if (list[bill_id].products.ElementAt(i).Value == quantity) { li = new KeyValuePair <string, int>(product, quantity); list[bill_id].products.Remove(li); products p = new products(); list[bill_id].total -= p.return_price(product) * quantity; if (list[bill_id].products.Count == 0) { list.Remove(bill_id); } FileStream format = new FileStream("bills.txt", FileMode.Truncate); format.Close(); FileStream file = new FileStream("bills.txt", FileMode.Append); for (int j = 0; j < list.Count; j++) { f.Serialize(file, list.ElementAt(j).Value); } file.Close(); list.Clear(); p.update_product(product, quantity, 0); return_message = "done"; list.Clear(); return(return_message); } else { int x = 0; x = list[bill_id].products.ElementAt(i).Value - quantity; li = new KeyValuePair <string, int>(product, list[bill_id].products.ElementAt(i).Value); list[bill_id].products.Remove(li); li = new KeyValuePair <string, int>(product, x); list[bill_id].products.Add(li); products p = new products(); list[bill_id].total -= p.return_price(product) * quantity; if (list[bill_id].products.Count == 0) { list.Remove(bill_id); } FileStream format = new FileStream("bills.txt", FileMode.Truncate); format.Close(); FileStream file = new FileStream("bills.txt", FileMode.Append); for (int j = 0; j < list.Count; j++) { f.Serialize(file, list.ElementAt(j).Value); } file.Close(); list.Clear(); return_message = "done"; p.update_product(product, quantity, 0); list.Clear(); return(return_message); } } else { return_message = "invalid quantity"; return(return_message); } } } return_message = "product not found"; return(return_message); } else { return_message = "over 14 day"; return(return_message); } } else { return_message = "invalid bill"; return(return_message); } } else { return_message = "invalid bill"; return(return_message); } }