示例#1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            var needsDbAction = false;

            var txtUser = txtUsername.Text;
            var active  = togActiveStat.Checked;
            var txtPass = txtPassword.Text;

            if (_userObj == null)
            {
                needsDbAction = true;
            }
            else
            {
                if (!txtUser.Equals(_userObj.Username))
                {
                    _userObj.Username = txtUser;
                    needsDbAction     = true;
                }

                if (active != _userObj.Active)
                {
                    _userObj.Active = active;
                    needsDbAction   = true;
                }

                if (_passwordModified)
                {
                    _userObj.Password = UserAccount.GetPasswordHash(txtPass);
                    needsDbAction     = true;
                }
            }


            if (needsDbAction)
            {
                using (var oH = new ObjectHelper(_dbSettings, _user))
                {
                    StatusObject dbActionStatus;
                    if (_userObj == null)
                    {
                        dbActionStatus = oH.AddUserAccount(txtUser, txtPass, active);
                        if (dbActionStatus.Success)
                        {
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                                "Failed to add this new user account. Please try again.");
                        }
                    }
                    else
                    {
                        dbActionStatus = oH.UpdateUserAccount(_userObj);
                        if (dbActionStatus.Success)
                        {
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                                "Failed to update this user account. Please try again.");
                        }
                    }
                }
            }
            Close();
        }