protected void btnSaveUpdate_Click(object sender, EventArgs e)
        {
            BM.Update();
            lblErrorMessageUpdate.Text = string.Empty;

            branch = BM.GetBranchByKey(long.Parse(gvBranchList.SelectedRow.Cells[2].Text));
            BM.PreviousBranch = BM.GetBranchByKey(long.Parse(gvBranchList.SelectedRow.Cells[2].Text));

            branch.AddressOne = txtAddressOneUpdate.Text;
            branch.AddressTwo = txtAddressTwoUpdate.Text;
            branch.BillingAddress = txtBillAddUpdate.Text;
            branch.BranchName = txtBranchNameUpdate.Text;
            branch.ContactPerson = txtContactPersonUpdate.Text;
            branch.FaxNumber = txtFaxNoUpdate.Text;
            branch.MobileNumber = txtMobileNoUpdate.Text;
            branch.PositionDescription = txtPositionUpdate.Text;
            branch.ShippingAddress = txtShipToAddUpdate.Text;
            branch.TelephoneNumber = txtTelNoUpdate.Text;
            branch.TIN = txtTinUpdate.Text;

            string sMessage = string.Empty;

            if (BM.ValidateData(branch, ref sMessage) == true)
            {
                BM.Save(branch);
                LoadAllBranch();
                ClearTextCtrls();
                EnableButtons();
            }
            else
            {
                lblErrorMessageUpdate.Text = sMessage;
                btnUpdateBranch_ModalPopupExtender.Show();
            }
        }
        public bool Delete(BranchClass branch)
        {
            bool bResult = false;

            try
            {
                using (DbManager db = new DbManager())
                {
                    Accessor.Query.Delete(db, branch);
                    bResult = true;
                }
            }
            catch(Exception except)
            {
                throw new System.ArgumentException(except.Message);
            }

            return bResult;
        }
        protected void btnSaveBranch_Click(object sender, EventArgs e)
        {
            ListItem litem = new ListItem();
            lblErrorMessage.Text = string.Empty;

            litem = dlAccounts.Items[dlAccounts.SelectedIndex];
            var branch = new BranchClass
            {
                AccountCode = litem.Value,
                BranchCode = txtBranchCode.Text,
                BranchName = txtBranchName.Text,
                AddressOne = txtAddressOne.Text,
                AddressTwo = txtAddressTwo.Text,
                ContactPerson = txtContactPerson.Text,
                PositionDescription = txtPosition.Text,
                TelephoneNumber = txtTelNo.Text,
                MobileNumber = txtMobileNo.Text,
                FaxNumber = txtFaxNo.Text,
                BillingAddress = txtBillingAdd.Text,
                ShippingAddress = txtShipToBillAdd.Text,
                TIN = txtTIN.Text
            };

            BM.Add();
            string sMessage = string.Empty;
            if (BM.ValidateData(branch, ref sMessage) == true)
            {
                BM.Save(branch);
                LoadAllBranch();
                ClearTextCtrls();
                EnableButtons();
            }
            else
            {
                lblErrorMessage.Text = sMessage;
                btnNewBranch_ModalPopupExtender.Show();
            }
        }
        public bool ValidateData(BranchClass branch, ref string Message)
        {
            bool bResult = false;

            if (string.IsNullOrEmpty(branch.AccountCode) == true)
            {
                Message = "Account name required!";
                return bResult;
            }

            if (string.IsNullOrEmpty(branch.BranchName) == true)
            {
                Message = "Branch name required!";
                return bResult;
            }

            int iResultCount = 0;
            bool bChange = true;

            if (OldBranch.AccountCode == branch.AccountCode || TransState == TransactionState.Add)
            {
                iResultCount = Accessor.BranchByBranchName(branch.AccountCode, branch.BranchName).Count;
                if (TransState == TransactionState.Update)
                {
                    if (OldBranch.BranchName == branch.BranchName)
                            bChange = false;
                }
            }

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

            /*
            string[] paramKey = new string[2];
            string[] paramColumn = new string[2];

            paramKey[0] = branch.BranchName;
            paramKey[1] = branch.BranchCode;
            paramColumn[0] = "BRANCH_NAME";
            paramColumn[1] = "BRANCH_CODE";
            int iResultCount = 0;
            bool bChange = true;

            iResultCount = Accessor.Query.SelectByKeyWords<BranchClass>(paramKey,paramColumn).Count;

            if (TransState == TransactionState.Update)
            {
                if (OldBranch.BranchName == branch.BranchName)
                    bChange = false;
            }

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

            paramKey = new string[1];
            paramColumn = new string[1];
            paramKey[0] = branch.BranchCode;
            paramColumn[0] = "BRANCH_CODE";
            iResultCount = 0;
            bChange = true;

            iResultCount = Accessor.Query.SelectByKeyWords<BranchClass>(paramKey, paramColumn).Count;

            if (TransState == TransactionState.Update)
            {
                if (OldBranch.BranchCode == branch.BranchCode)
                    bChange = false;
            }

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

            bResult = true;
            return bResult;
        }
 public bool Save(BranchClass branch)
 {
     bool bResult = false;
     branch.BranchCode = GenerateBranchCode();
     using (DbManager db = new DbManager())
     {
         try
         {
             if (branch.RecordNo != 0)
             {
                 Accessor.Query.Update(db, branch);
             }
             else
             {
                 Accessor.Query.Insert(db, branch);
             }
             bResult = true;
         }
         catch (Exception except)
         {
             throw new System.ArgumentException(except.Message);
         }
     }
     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 gvBranchList_SelectedIndexChanged(object sender, EventArgs e)
        {
            AccountsManager AM = new AccountsManager();
            branch = BM.GetBranchByKey(long.Parse(gvBranchList.SelectedRow.Cells[2].Text));

            txtAccountNameUpdate.Text = AM.GetAccountByAccountCode(branch.AccountCode).AccountName;
            txtAddressOneUpdate.Text = branch.AddressOne;
            txtAddressTwoUpdate.Text = branch.AddressTwo;
            txtBillAddUpdate.Text = branch.BillingAddress;
            txtBranchCodeUpdate.Text = branch.BranchCode;
            txtBranchNameUpdate.Text = branch.BranchName;
            txtContactPersonUpdate.Text = branch.ContactPerson;
            txtFaxNoUpdate.Text = branch.FaxNumber;
            txtMobileNoUpdate.Text = branch.MobileNumber;
            txtPositionUpdate.Text = branch.PositionDescription;
            txtShipToAddUpdate.Text = branch.ShippingAddress;
            txtTelNoUpdate.Text = branch.TelephoneNumber;
            txtTinUpdate.Text = branch.TIN;
        }