private void SubFormCTPN_FormClosing(object sender, FormClosingEventArgs e) { if (flagSuccess == false) { CTPN_BDS.CancelEdit(); } Program.formMain.Enabled = true; }
private void btnOK_Click(object sender, EventArgs e) { if (!checkValidate(tbMaPN, "Mã Phiếu Nhập is not empty!")) { return; } if (!checkValidate(tbMaVT, "Mã VT is not empty!")) { return; } //Kiểm tra trùng CTPN int positionMaVT = CTPN_BDS.Find("MAVT", tbMaVT.Text); if (positionMaVT != -1 && (positionMaVT != CTPN_BDS.Position)) { MessageBox.Show("Chi tiết Đơn Đặt Hàng này đã được lập Chi Tiết Phiếu Nhập!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DialogResult dr = MessageBox.Show("Bạn có chắc muốn ghi dữ liệu vào Database?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { try { //Lưu lại MAVT và SOLUONG để khi EndEdit không bị mất dữ liệu vì con trỏ về đầu string maVT = tbMaVT.Text; int soLuong = int.Parse(nuSoLuong.Value.ToString()); this.CTPN_BDS.EndEdit(); this.cTPNTableAdapter.Update(Program.formNhanVien.getDataSet().CTPN); string query = "DECLARE @result int " + "EXEC @result = SP_UpdateVatTu @p1, @p2, @p3 " + "SELECT 'result' = @result"; SqlCommand sqlCommand = new SqlCommand(query, Program.connection); sqlCommand.Parameters.AddWithValue("@p1", maVT); sqlCommand.Parameters.AddWithValue("@p2", soLuong); sqlCommand.Parameters.AddWithValue("@p3", "INCREASE"); SqlDataReader dataReader = null; try { dataReader = sqlCommand.ExecuteReader(); } catch (Exception ex) { MessageBox.Show("Lỗi khi cập nhật Vật Tư vào Database!\n" + ex.Message, "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error); dataReader.Close(); return; } dataReader.Read(); int result = int.Parse(dataReader.GetValue(0).ToString()); dataReader.Close(); if (result == 0) { MessageBox.Show("Lỗi khi cập nhật Vật Tư vào Database!\n", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error); query = "DECLARE @result int " + "EXEC @result = SP_UpdateVatTu @p1, @p2, @p3 " + "SELECT 'result' = @result"; sqlCommand = new SqlCommand(query, Program.connection); sqlCommand.Parameters.AddWithValue("@p1", maVT); sqlCommand.Parameters.AddWithValue("@p2", soLuong); sqlCommand.Parameters.AddWithValue("@p3", "DECREASE"); try { dataReader = sqlCommand.ExecuteReader(); } catch (Exception ex) { MessageBox.Show("Lỗi Rollback dữ liệu. Vui lòng truy suất Database kiểm tra lại!\n" + ex.Message, "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error); dataReader.Close(); return; } return; } flagSuccess = true; this.Close(); } catch (Exception ex) { MessageBox.Show("Ghi dữ liệu thất lại. Vui lòng kiểm tra lại!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }