예제 #1
0
        private string opc = "";  //Declarando opc

        private void iniciarOpc() //Declarando iniciarOpc
        {
            switch (opc)
            {
            case "Novo":     //Configurando função novo
                HabilitarCampos();
                LimparCampos();
                break;

            case "Buscar":     //Configurando função Buscar
                try
                {
                    objtabela.Nome = txtNomeAluno.Text;
                    List <AlunoEnt> Lista = new List <AlunoEnt>();
                    Lista = new AlunoModel().Buscar(objtabela);
                    GridAluno.AutoGenerateColumns = false;
                    GridAluno.DataSource          = Lista;
                }

                catch (Exception ex)     //Caso ocorra erro
                {
                    MessageBox.Show("Erro ao listar dados" + ex.Message);
                }

                break;

            case "Salvar":     //Configurando função salvar
                try
                {
                    objtabela.Nome          = txtNomeAluno.Text;
                    objtabela.Cpf           = txtCpfAluno.Text;
                    objtabela.Rg            = txtRgAluno.Text;
                    objtabela.Telefone      = txtTelefoneAluno.Text;
                    objtabela.Categoriacnh  = txtCategoriacnhAluno.Text;
                    objtabela.Horariodeaula = txtHorarioDeAulaAluno.Text;

                    if (txtNomeAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    if (txtCpfAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    if (txtRgAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    if (txtTelefoneAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    if (txtCategoriacnhAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    if (txtHorarioDeAulaAluno.Text == "")     //Verificação de campos vazios
                    {
                        MessageBox.Show("Preencha todos os dados!");
                        return;
                    }

                    int x = AlunoModel.Inserir(objtabela);
                    if (x > 0)
                    {
                        MessageBox.Show(String.Format("Aluno {0} salvo com sucesso", txtNomeAluno.Text));     //Cadastrando aluno
                    }

                    else
                    {
                        MessageBox.Show("Dado não inserido!");
                    }
                }
                catch (Exception ex)     //Caso ocorra erro
                {
                    MessageBox.Show("Ocorreu um erro" + ex.Message);
                    throw;
                }

                DesabilitarCampos();
                LimparCampos();
                break;

            case "Excluir":     //Configurando função excluir
                try
                {
                    objtabela.Id = (txtIdAluno.Text);

                    int x = AlunoModel.Excluir(objtabela);
                    if (x > 0)
                    {
                        MessageBox.Show(String.Format("Aluno {0} excluído com sucesso", txtNomeAluno.Text));     //Excluindo aluno
                    }

                    else
                    {
                        MessageBox.Show("Não excluído!");
                    }
                }

                catch (Exception ex)     //Caso ocorra erro
                {
                    MessageBox.Show("Ocorreu um erro ao excluir. Error" + ex.Message);
                    throw;
                }
                break;

            case "Editar":
                try
                {
                    objtabela.Id            = txtIdAluno.Text;
                    objtabela.Nome          = txtNomeAluno.Text;
                    objtabela.Cpf           = txtCpfAluno.Text;
                    objtabela.Rg            = txtRgAluno.Text;
                    objtabela.Telefone      = txtTelefoneAluno.Text;
                    objtabela.Categoriacnh  = txtCategoriacnhAluno.Text;
                    objtabela.Horariodeaula = txtHorarioDeAulaAluno.Text;

                    int x = AlunoModel.Editar(objtabela);
                    if (x > 0)
                    {
                        MessageBox.Show(String.Format("Aluno {0} editado com sucesso", txtNomeAluno.Text));     //Editando aluno
                    }

                    else
                    {
                        MessageBox.Show("Dado não atualizado!");
                    }
                }
                catch (Exception ex)     //Caso ocorra erro
                {
                    MessageBox.Show("Ocorreu um erro ao editar" + ex.Message);
                    throw;
                }

                break;

            default:
                break;
            }
        }