private void add_new_btn_Click(object sender, EventArgs e) { fieldcheck(); if (check == false) { MessageBox.Show("Please complete the field!"); } else { string saveimg = ""; SqlConnection conn = new SqlConnection(connString); SqlTransaction trans = default; ImageServices imgs = new ImageServices(opnfdFn); imgs.storeImage(); saveimg = imgs.createdFileName; try { conn = new SqlConnection(connString); conn.Open(); trans = conn.BeginTransaction(); using (var cmd = new SqlCommand()) { cmd.Connection = conn; cmd.Transaction = trans; cmd.CommandText = @"INSERT INTO Menus (menu_id, menu_name, menu_price, menu_description, menu_img) VALUES (@id, @name, @price, @des, @img)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", this.menu_id_txtbox.Text.Trim()); cmd.Parameters.AddWithValue("@name", this.menu_name_txtbox.Text.Trim()); cmd.Parameters.AddWithValue("@price", this.menu_price_txtbox.Text.Trim()); cmd.Parameters.AddWithValue("@des", this.menu_desc_txtbox.Text.Trim()); cmd.Parameters.AddWithValue("@img", saveimg); cmd.ExecuteNonQuery(); } trans.Commit(); MessageBox.Show("Add Success. Please re-open."); } catch (Exception ex) { if (trans != null) { trans.Rollback(); } MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (trans != null) { trans.Dispose(); } if (conn != null) { conn.Close(); } } } }