public int CheckUpdateAccount(int id, string user, string pass, int a_type) { DBManager db = new DBManager(); int contactCount = db.GetContactCountByAccountId(id); if (contactCount == 0) { AccountDB acc = db.GetAccountByUserNameAndAccountTypeId(user, a_type); int accId = acc.Id; accId = accId > 0 ? accId : id; return (db.UpdateAccountById(accId, user, pass, a_type) > 0 ? accId : -1); } else { AccountDB account = db.GetAccountById(id); if (account.Username.Equals(user) && account.AccountTypeId == a_type) { return (db.UpdateAccountById(id, user, pass, a_type) > 0 ? id : -1); } else { AccountDB acc = db.GetAccountByUserNameAndAccountTypeId(user, a_type); int accId = acc.Id; if (accId > 0) { return (db.UpdateAccountById(accId, user, pass, a_type) > 0 ? accId : -1); } else { return db.AddNewAccount(user, pass, a_type); } } } }