Beispiel #1
0
        private void InsertOrUpdateCurrentValueToDatabase(bool isUpdate = false)
        {
            string maLop = txtMaLopHoc.Text;
            string maKH  = cbMaKhoaHoc.Text;

            string maGV = GetEqualName(cbGiangVien.Text, dtGiangVien, 0, 1);

            DateTime ngayBD = dateNgayBatDau.Value;

            DateTime ngayKT = dateNgayKetThuc.Value;

            string maHP = GetEqualName(cbHocPhi.Text, dtHocPhi, 0, 1);

            int siSo;

            try
            {
                siSo = int.Parse(txtSiSo.Text);
            }
            catch (Exception e)
            {
                DialogHelper.ShowErrorDialog("Sĩ số nhập vào không hợp lệ!");
                return;
            }

            // điền thiếu thì báo lỗi
            if (maLop == "" ||
                maKH == "" ||
                maGV == "" ||
                maHP == "" ||
                siSo == 0)
            {
                DialogHelper.ShowMissingField();
                return;
            }


            if (isUpdate)
            {
                var row = GetCurrentRow();
                if (row == null)
                {
                    DialogHelper.ShowMissingSelectedRow();
                    return;
                }

                string _maLopRow = row.Cells["MALOP"].Value.ToString();

                if (maLop != _maLopRow)
                {
                    DialogHelper.ShowCannotModifiyPrimaryKey();
                    return;
                }
            }


            bool isSuccess = true;

            LopHoc    lophoc  = new LopHoc();
            CT_LopHoc chitiet = new CT_LopHoc();

            if (!isUpdate)
            {
                isSuccess = lophoc.insert(AccountHelper.getAccountId(), AccountHelper.getAccoutPassword(), maLop, maKH, ngayBD.Date, ngayKT.Date, siSo, maHP) == 1;
                chitiet.insert(AccountHelper.getAccountId(), AccountHelper.getAccoutPassword(), maLop, maGV);
            }
            else
            {
                isSuccess = lophoc.update(AccountHelper.getAccountId(), AccountHelper.getAccoutPassword(), maLop, maKH, ngayBD.Date, ngayKT.Date, siSo, maHP) == 1;


                string oldGV = GetEqualName(maLop, dtChiTiet, 0, 1);
                // nếu có thay giảng viên
                if (!oldGV.Equals(maGV))
                {
                    isSuccess &= chitiet.delete(AccountHelper.getAccountId(), AccountHelper.getAccoutPassword(), maLop, oldGV) == 1;
                    isSuccess &= chitiet.insert(AccountHelper.getAccountId(), AccountHelper.getAccoutPassword(), maLop, maGV) == 1;
                }
            }


            if (isSuccess)
            {
                InitChiTiet();
                ShowTable();
                ClearField();
                return;
            }
            else
            {
                DialogHelper.ShowErrorOnUpdate();
                return;
            }
        }