private void EditProduct_Load(object sender, EventArgs e) { Nametxtbx22.Focus(); }
private void Updatebtn_Click(object sender, EventArgs e) { source1.Conn.Close(); string command55 = "SELECT id FROM drugsbasic2 WHERE name = @name"; MySqlCommand Comm55 = new MySqlCommand(command55, source1.Conn); Comm55.Parameters.AddWithValue("@name", Activeingredienttxtbx.Text); source1.MySqlCon(); MySqlDataReader Reader55 = Comm55.ExecuteReader(); if (Reader55.HasRows) { Reader55.Read(); foreigID = Reader55["id"].ToString(); source1.Conn.Close(); string command1 = "SELECT id, productname FROM products WHERE id = @id"; MySqlCommand Comm1 = new MySqlCommand(command1, source1.Conn); Comm1.Parameters.AddWithValue("@id", ProductIdtxtbx.Text); source1.MySqlCon(); MySqlDataReader Reader1 = Comm1.ExecuteReader(); Reader1.Read(); idnorepeat = Reader1["id"].ToString(); repeatedname = Reader1["productname"].ToString(); source1.Conn.Close(); //} if (idnorepeat == ProductIdtxtbx.Text && repeatedname == Nametxtbx22.Text && barcodetxtbx.Text != "") { source1.Conn.Close(); source1.MySqlCon(); MySqlCommand Icom1 = source1.Conn.CreateCommand(); Icom1.CommandText = "UPDATE products SET productname = @productname, barcode = @barcode, foreignID = @foreignID WHERE id = @id"; Icom1.Parameters.AddWithValue("@productname", Nametxtbx22.Text); Icom1.Parameters.AddWithValue("@barcode", barcodetxtbx.Text); Icom1.Parameters.AddWithValue("@foreignID", foreigID); Icom1.Parameters.AddWithValue("@id", ProductIdtxtbx.Text); Icom1.ExecuteNonQuery(); source1.Conn.Close(); DialogResult Updated = MessageBox.Show("This product is successfully updated!", "Successful update", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Updated == DialogResult.OK) { Close(); //MainPage.AutoCompleteName(); } } else if (idnorepeat != ProductIdtxtbx.Text && barcodetxtbx.Text != "") { source1.Conn.Close(); string command12 = "SELECT productname FROM products WHERE barcode = @barcode"; MySqlCommand Comm12 = new MySqlCommand(command12, source1.Conn); Comm12.Parameters.AddWithValue("@barcode", barcodetxtbx.Text); source1.MySqlCon(); MySqlDataReader Reader12 = Comm12.ExecuteReader(); Reader12.Read(); string repeatedbarcodename = Reader12["productname"].ToString(); source1.Conn.Close(); MessageBox.Show("This barcode is assigned to another product!" + "\n" + repeatedbarcodename, "Barcode error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (idnorepeat == ProductIdtxtbx.Text && repeatedname == Nametxtbx22.Text && barcodetxtbx.Text == "") { source1.Conn.Close(); source1.MySqlCon(); MySqlCommand Icom1 = source1.Conn.CreateCommand(); Icom1.CommandText = "UPDATE products SET productname = @productname, barcode = NULL ,foreignID = @foreignID WHERE id = @id"; Icom1.Parameters.AddWithValue("@productname", Nametxtbx22.Text); Icom1.Parameters.AddWithValue("@foreignID", foreigID); Icom1.Parameters.AddWithValue("@id", ProductIdtxtbx.Text); Icom1.ExecuteNonQuery(); source1.Conn.Close(); DialogResult Updated = MessageBox.Show("This product is successfully updated!", "Successful update", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Updated == DialogResult.OK) { Close(); //MainPage.AutoCompleteName(); } } else if (idnorepeat == ProductIdtxtbx.Text && repeatedname != Nametxtbx22.Text && barcodetxtbx.Text != "") { DialogResult changename = MessageBox.Show("Are you sure you want to change the name of this product?", "Record name change", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); if (changename == DialogResult.Yes) { source1.Conn.Close(); source1.MySqlCon(); MySqlCommand Icom1 = source1.Conn.CreateCommand(); Icom1.CommandText = "UPDATE products SET productname = @productname, barcode = @barcode, foreignID = @foreignID WHERE id = @id"; Icom1.Parameters.AddWithValue("@productname", Nametxtbx22.Text); Icom1.Parameters.AddWithValue("@barcode", barcodetxtbx.Text); Icom1.Parameters.AddWithValue("@foreignID", foreigID); Icom1.Parameters.AddWithValue("@id", ProductIdtxtbx.Text); Icom1.ExecuteNonQuery(); source1.Conn.Close(); DialogResult Updated = MessageBox.Show("This product is successfully updated!", "Successful update", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Updated == DialogResult.OK) { Close(); //MainPage.AutoCompleteName(); } } else if (changename == DialogResult.No) { Nametxtbx22.Focus(); } else if (changename == DialogResult.Cancel) { Close(); } } else if (idnorepeat == ProductIdtxtbx.Text && repeatedname != Nametxtbx22.Text && barcodetxtbx.Text == "") { DialogResult changename = MessageBox.Show("Are you sure you want to change the name of this product?", "Record name change", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); if (changename == DialogResult.Yes) { source1.Conn.Close(); source1.MySqlCon(); MySqlCommand Icom1 = source1.Conn.CreateCommand(); Icom1.CommandText = "UPDATE products SET productname = @productname, foreignID = @foreignID WHERE id = @id"; Icom1.Parameters.AddWithValue("@productname", Nametxtbx22.Text); Icom1.Parameters.AddWithValue("@foreignID", foreigID); Icom1.Parameters.AddWithValue("@id", ProductIdtxtbx.Text); Icom1.ExecuteNonQuery(); source1.Conn.Close(); DialogResult Updated = MessageBox.Show("This product is successfully updated!", "Successful update", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Updated == DialogResult.OK) { Close(); } } else if (changename == DialogResult.No) { Nametxtbx22.Focus(); } else if (changename == DialogResult.Cancel) { Close(); } } } else { MessageBox.Show("Enter a valid active ingredient name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Activeingredienttxtbx.Focus(); source1.Conn.Close(); } }