Exemplo n.º 1
0
        private void btnGravar_Click(object sender, EventArgs e)
        {
            if (!Valida.EstaPreenchido(txtNome.Text))
            {
                txtNome.Focus();
                Msg.MsgErro("Preencha o nome corretamente.");
                return;
            }

            if (!Valida.EstaPreenchido(txtLogin.Text))
            {
                txtLogin.Focus();
                Msg.MsgErro("Preencha o login corretamente.");
                return;
            }

            if (!Valida.EstaPreenchido(txtEmail.Text))
            {
                txtEmail.Focus();
                Msg.MsgErro("Preencha o email corretamente.");
                return;
            }

            if (!Valida.EstaPreenchido(txtSenha.Text))
            {
                txtSenha.Focus();
                Msg.MsgErro("Preencha a senha corretamente.");
                return;
            }

            if (!Valida.EstaPreenchido(txtConfirmacao.Text))
            {
                txtConfirmacao.Focus();
                Msg.MsgErro("Preencha a confirmação da senha corretamente.");
                return;
            }

            if (!Valida.ComparaValores(txtSenha.Text, txtConfirmacao.Text))
            {
                txtSenha.Focus();
                Msg.MsgErro("Senha e confirmação estão diferentes.");
                return;
            }

            // Carregando a entidade
            Usuario.NomeUsuario      = txtNome.Text;
            Usuario.EmailUsuario     = txtEmail.Text;
            Usuario.LoginUsuario     = txtLogin.Text;
            Usuario.SenhaUsuario     = txtSenha.Text;
            Usuario.BloqueadoUsuario = (chkBloqueado.Checked) ? true : false;
            Usuario.AtivoUsuario     = (chkAtivo.Checked) ? true : false;

            // se novo registro
            if (novoRegistro)
            {
                // Se cadastro ocorreu corretamente
                if (objBu.Cadastrar(Usuario))
                {
                    Msg.MsgAlerta("Dados cadastrados com sucesso.");
                    LogFile.RegistraLog("Usuario cadastrado.");
                    Finaliza();
                }
                else
                {
                    Msg.MsgAlerta("Falha na gravação dos dados.");
                    LogFile.RegistraLog("Erro no cadastro de usuario.");
                }
            }
            else
            {
                Usuario.CodUsuario = Convert.ToInt32(txtCodUsuario.Text);
                // Se atualizacao ocorreu corretamente
                if (objBu.Gravar(Usuario))
                {
                    Msg.MsgAlerta("Dados salvos com sucesso.");
                    LogFile.RegistraLog("Usuario alterado.");
                    Finaliza();
                }
                else
                {
                    Msg.MsgAlerta("Falha na gravação dos dados.");
                    LogFile.RegistraLog("Erro na gravação de usuario.");
                }
            }
        }