public static int Update(tblDeclarationLoan loan)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     loan.ModifiedDate = CommonFactory.GetCurrentDate();
     // Get orgin object
     var objOrginDeclaration = db.tblDeclarationLoans.Where(g => g.ID.Equals(loan.ID)).FirstOrDefault();
     db.Attach(objOrginDeclaration);
     db.ApplyPropertyChanges("tblDeclarationLoans", loan);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 public static int Insert(tblDeclarationLoan loan)
 {
     dbEcustomEntities _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     _db.Connection.Open();
     loan.CreatedDate = CommonFactory.GetCurrentDate();
     loan.ModifiedDate = CommonFactory.GetCurrentDate();
     _db.AddTotblDeclarationLoans(loan);
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         return _db.SaveChanges();
     }
     catch (Exception ex)
     {
         return -1;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtBorrower.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên người mượn không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            tblDeclaration declaration = DeclarationFactory.GetByID(_declarationID);
            if (declaration == null)
            {
                MessageBox.Show("Tờ khai không còn tồn tại, xin kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (declaration != null && declaration.LoanStatus==true)
            {
                MessageBox.Show("Hồ sơ về tờ khai đã được cho mượn rồi, xin kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            tblDeclarationLoan loan = new tblDeclarationLoan();
            loan.CreatedUserID = _userInfo.UserID;
            loan.ModifiedUserID = _userInfo.UserID;
            loan.LoanDate = pkLoanDate.Value;
            loan.LoanReason = txtLoanReason.Text.Trim();
            loan.GetLoanDescription = txtDescription.Text.Trim();
            loan.BorrowerName = txtBorrower.Text.Trim();
            loan.LenderUserID = _userInfo.UserID;
            loan.DeclarationID = declaration.DeclarationID;
            int updateLoan = 0;
            try
            {

                declaration.LoanStatus = true;
                declaration.ModifiedByID = _userInfo.UserID;
                int updateDeclaration = DeclarationFactory.Update(declaration);
                if (updateDeclaration > 0)
                {
                    updateLoan = DeclarationLoanFactory.Insert(loan);
                }

                if (updateLoan > 0)
                {
                    MessageBox.Show("Thêm mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (_frmParent != null)
                    {
                        _frmParent.search();
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Thêm mới không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thêm mới không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

                
        }