private void buttom_confirm_Click(object sender, EventArgs e) { conn.Open(); SqlCommand cmd = new SqlCommand("select TinhTrang from HoaDonMua where MaHDMua=@mahd", conn); cmd.Parameters.AddWithValue("@mahd", id.Text); SqlDataReader r = cmd.ExecuteReader(); if (r.Read()) { if ((string)r["TinhTrang"] == "Chưa xác nhận") { if (idemp != idngnhan.Text && idngnhan.Text.Length != 0) { Confirm f = new Confirm("MB_DIRECTOR"); f.ShowDialog(); if (f.DialogResult == DialogResult.OK) { DateTime date = DateTime.Today; day2.Text = date.Day.ToString(); month2.Text = date.Month.ToString(); year2.Text = date.Year.ToString(); r.Close(); cmd.CommandText = "update HoaDonMua set TinhTrang=@tt,NguoiNhan=@ngnhan,NgayNhan=@day where MaHDMua=@mahd"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@mahd", id.Text); cmd.Parameters.AddWithValue("@tt", "Đã xác nhận"); cmd.Parameters.AddWithValue("@day", month2.Text + "/" + day2.Text + "/" + year2.Text); cmd.Parameters.AddWithValue("@ngnhan", idemp); cmd.ExecuteNonQuery(); tinhtrang.Text = "Đã xác nhận"; idngnhan.Text = idemp; ngnhan.Text = name; DataRow[] rows = dt.Select("MaHDMua = '" + id.Text + "'"); for (int i = 0; i < rows.Length; i++) { rows[i]["TinhTrang"] = "Đã xác nhận"; rows[i]["NguoiNhan"] = idemp; } error.Text = "Confirm success!"; buttom_change.Enabled = false; buttom_change.BackgroundImage = Image.FromFile(@"Image\buttom_change3.png"); buttom_confirm.Visible = false; buttom_unconfirm.Visible = true; conn.Close(); //Cập nhật kho hàng SqlDataAdapter adapter = new SqlDataAdapter("select * from ChiTietHoaDonMua where MaHDMua='" + id.Text + "'", conn); DataTable dtsp = new DataTable(); adapter.Fill(dtsp); adapter.Dispose(); conn.Open(); foreach (DataRow row in dtsp.Rows) { cmd.CommandText = "update SanPham set SLTon=SLTon+" + (int)row["SLMua"] + ",GiaMua=" + (int)row["GiaMua"] + "where MaSP='" + (string)row["MaSP"] + "'"; cmd.ExecuteNonQuery(); } conn.Close(); } } else { DateTime date = DateTime.Today; day2.Text = date.Day.ToString(); month2.Text = date.Month.ToString(); year2.Text = date.Year.ToString(); r.Close(); cmd.CommandText = "update HoaDonMua set TinhTrang=@tt,NguoiNhan=@ngnhan,NgayNhan=@day where MaHDMua=@mahd"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@mahd", id.Text); cmd.Parameters.AddWithValue("@tt", "Đã xác nhận"); cmd.Parameters.AddWithValue("@day", month2.Text + "/" + day2.Text + "/" + year2.Text); cmd.Parameters.AddWithValue("@ngnhan", idemp); cmd.ExecuteNonQuery(); tinhtrang.Text = "Đã xác nhận"; idngnhan.Text = idemp; ngnhan.Text = name; DataRow[] rows = dt.Select("MaHDMua = '" + id.Text + "'"); for (int i = 0; i < rows.Length; i++) { rows[i]["TinhTrang"] = "Đã xác nhận"; rows[i]["NguoiNhan"] = idemp; } error.Text = "Confirm success!"; buttom_change.Enabled = false; buttom_change.BackgroundImage = Image.FromFile(@"Image\buttom_change3.png"); buttom_confirm.Visible = false; buttom_unconfirm.Visible = true; conn.Close(); //Cập nhật kho hàng SqlDataAdapter adapter = new SqlDataAdapter("select * from ChiTietHoaDonMua where MaHDMua='" + id.Text + "'", conn); DataTable dtsp = new DataTable(); adapter.Fill(dtsp); adapter.Dispose(); conn.Open(); foreach (DataRow row in dtsp.Rows) { cmd.CommandText = "update SanPham set SLTon=SLTon+" + (int)row["SLMua"] + ",GiaMua=" + (int)row["GiaMua"] + "where MaSP='" + (string)row["MaSP"] + "'"; cmd.ExecuteNonQuery(); } conn.Close(); } } else { MessageBox.Show("Hóa đơn này đã được xác nhận!", "Waring", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } r.Close(); conn.Close(); }