public int Update(Q_UserCommandReadSound model)
 {
     using (db = new QMSSystemEntities())
     {
         var obj = db.Q_UserCommandReadSound.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id);
         if (obj != null)
         {
             if (!CheckExists(model))
             {
                 obj.UserId         = model.UserId;
                 obj.CommandId      = model.CommandId;
                 obj.Index          = model.Index;
                 obj.ReadTemplateId = model.ReadTemplateId;
                 obj.Note           = model.Note;
                 db.SaveChanges();
                 return(obj.Id);
             }
             else
             {
                 return(0);
             }
         }
         return(0);
     }
 }
        private void gridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                int Id = 0;
                int.TryParse(gridView.GetRowCellValue(gridView.FocusedRowHandle, "Id").ToString(), out Id);
                if (Id == 0 && string.IsNullOrEmpty(gridView.GetRowCellValue(gridView.FocusedRowHandle, "Index").ToString()))
                {
                    goto End;
                }
                else if (Id == 0 && string.IsNullOrEmpty(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ReadTemplateId").ToString()))
                {
                    goto End;
                }
                else if (Id != 0 && string.IsNullOrEmpty(gridView.GetRowCellValue(gridView.FocusedRowHandle, "Index").ToString()))
                {
                    MessageBox.Show("Vui lòng nhập số thứ tự ưu tiên.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (Id != 0 && string.IsNullOrEmpty(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ReadTemplateId").ToString()))
                {
                    MessageBox.Show("Vui lòng chọn mẫu đọc âm thanh.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    var obj = new Q_UserCommandReadSound();
                    obj.Id             = Id;
                    obj.UserId         = userId;
                    obj.CommandId      = cmdId;
                    obj.Index          = (int)gridView.GetRowCellValue(gridView.FocusedRowHandle, "Index");
                    obj.ReadTemplateId = (int)gridView.GetRowCellValue(gridView.FocusedRowHandle, "ReadTemplateId");
                    obj.Note           = gridView.GetRowCellValue(gridView.FocusedRowHandle, "Note") != null?gridView.GetRowCellValue(gridView.FocusedRowHandle, "Note").ToString() : "";

                    int kq = 0;
                    if (obj.Id == 0)
                    {
                        kq = BLLUserCmdReadSound.Instance.Insert(connect, obj);
                    }
                    else
                    {
                        kq = BLLUserCmdReadSound.Instance.Update(connect, obj);
                    }

                    if (kq == 0)
                    {
                        MessageBox.Show("Tên chính sách này đã tồn tại. Xin nhập tên khác.", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        goto End;
                    }
                    else
                    {
                        GetGrid();
                        frmMain.lib_UserCMDReadSound = BLLUserCmdReadSound.Instance.Gets(connect);
                    }
                }
            }
            catch (Exception ex)
            { }
            End : { }
        }
 public int Insert(Q_UserCommandReadSound obj)
 {
     using (db = new QMSSystemEntities())
     {
         if (!CheckExists(obj))
         {
             db.Q_UserCommandReadSound.Add(obj);
             db.SaveChanges();
         }
         return(obj.Id);
     }
 }
        private bool CheckExists(Q_UserCommandReadSound model)
        {
            var obj = db.Q_UserCommandReadSound.FirstOrDefault(x => !x.IsDeleted && x.Id != model.Id && x.UserId == model.UserId && x.CommandId == model.CommandId && x.ReadTemplateId == model.ReadTemplateId);

            return(obj != null ? true : false);
        }