public bool Delete(AccountClass account)
 {
     bool bResult = false;
     try
     {
         using (DbManager db = new DbManager())
         {
             Accessor.Query.Delete(db, account);
         }
         bResult = true;
     }
     catch (Exception except)
     {
         throw new System.ArgumentException(except.Message);
     }
     TransState = TransactionState.Default;
     return bResult;
 }
        protected void btnSaveUpdate_Click(object sender, EventArgs e)
        {
            AM.Update();
            AM.PreviousAccount = AM.GetAccountByKey(long.Parse(gvAccountList.SelectedRow.Cells[2].Text));
            accounts = AM.GetAccountByKey(long.Parse(gvAccountList.SelectedRow.Cells[2].Text));
            accounts.AccountCode = this.txtAccountCodeUpdate.Text.ToUpper();
            accounts.AccountName = this.txtAccountNameUpdate.Text.ToUpper();

            string sMessage = string.Empty;
            if (AM.ValidateAccount(accounts, ref sMessage) == true)
            {
                AM.Save(accounts);
                ClearTextControl();
                LoadAllAccounts();
            }
            else
            {
                lblErrorMessageUpdate.Text = sMessage.ToUpper();
                btnUpdateAccount_ModalPopupExtender.Show();
            }
        }
        protected void btnSaveAccount_Click(object sender, EventArgs e)
        {
            AM.Add();
            var accounts = new AccountClass
            {
                AccountCode = string.Empty,
                AccountName = this.txtAccountName.Text.ToUpper()
            };

            string sMessage = string.Empty;
            if (AM.ValidateAccount(accounts, ref sMessage) == true)
            {
                AM.Save(accounts);
                ClearTextControl();
                LoadAllAccounts();
                //ResponseMessage("Account has been saved!");
            }
            else
            {
                lblErrorMessage.Text = sMessage.ToUpper();
                btnNewAccount_ModalPopupExtender.Show();
            }
        }
        public bool Save(AccountClass account)
        {
            bool bResult = false;
            using (DbManager db = new DbManager())
            {
                try
                {
                    if (account.RecordNo != 0)
                    {
                        Accessor.Query.Update(db, account);
                    }
                    else
                    {
                        account.AccountCode = GenerateAccountCode();
                        Accessor.Query.Insert(db, account);
                    }
                    bResult = true;
                }
                catch (Exception except)
                {
                    throw new System.ArgumentException(except.Message);
                }

            }
            TransState = TransactionState.Default;
            return bResult;
        }
        public bool ValidateAccount(AccountClass account, ref string Message)
        {
            bool bResult = false;
            bool bChange = true;
            int iResultCount = 0;

            iResultCount = Accessor.AccountsByAccountCode(account.AccountCode).Count;
            if (TransState == TransactionState.Update)
            {
                if (OldAccount.AccountCode == account.AccountCode)
                    bChange = false;
            }

            if (iResultCount > 0 && bChange == true)
            {
                Message = "Account code already been used!";
                return bResult;
            }

            iResultCount = Accessor.AccountsByAccountName(account.AccountCode, account.AccountName).Count;
            bChange = true;

            if (TransState == TransactionState.Update)
            {
                if (OldAccount.AccountName == account.AccountName)
                    bChange = false;
            }

            if (iResultCount > 1 && bChange == true)
            {
                Message = "Account name already exist!";
                return bResult;
            }

            bResult = true;
            return bResult;
        }
        private void GetObjectsFromDropList()
        {
            if (dlAccounts.SelectedIndex > 0)
            {
                AccountClass accountSelect = new AccountClass();
                accountSelect = AM.GetAccountByAccountCode(dlAccounts.SelectedValue);
                account = accountSelect;
                //CIM.AccountObject = accountSelect;
                OM.AccountObject = accountSelect;
            }

            if (dlBranch.SelectedIndex > -1)
            {
                BranchClass branchSelect = new BranchClass();
                branchSelect = BM.GetBranchByBranchCode(dlBranch.SelectedValue);
                branch = branchSelect;
                //CIM.BranchObject = branchSelect;
                OM.BranchObject = branchSelect;
            }

            if (dlBrand.SelectedIndex > 0)
            {
                Brand brandlocal = new Brand();

                brandlocal = BrM.GetBrandByBrandCode(dlBrand.SelectedValue);
                brand = brandlocal;
                //CIM.BrandObject = brandlocal;
                OM.BrandObject = brandlocal;
            }
        }
 protected void gvAccountList_SelectedIndexChanged(object sender, EventArgs e)
 {
     accounts = AM.GetAccountByKey(long.Parse(gvAccountList.SelectedRow.Cells[2].Text));
     AM.PreviousAccount = AM.GetAccountByKey(long.Parse(gvAccountList.SelectedRow.Cells[2].Text));
     txtAccountCodeUpdate.Text = accounts.AccountCode;
     txtAccountNameUpdate.Text = accounts.AccountName;
 }