private void Cadastrar() { if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) { DialogResult r = MessageBox.Show("Deseja adicionar um veiculo a esta pessoa ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (r == DialogResult.Yes) { //MPessoa item = (MPessoa)dataGridView1.SelectedRows[0].DataBoundItem; //string nome = item.Nome; //string cpf = item.CPF; //MessageBox.Show(cpf); //new VVeiculoCadastro().ShowDialog(); MPessoa item = new MPessoa(); item.CPF = dataGridView1.SelectedRows[0]. Cells["cPFDataGridViewTextBoxColumn"].Value.ToString(); VVeiculoCadastro v = new VVeiculoCadastro(item); v.ShowDialog(); } } }
//ATUALIZAR public static void Atualizar(MPessoa item) { if (item == null) { throw new Exception("Objeto PESSOA inválido"); } if (item.Nome.Trim() == "" || item.Nome.Length > 200) { throw new Exception("Nome digitado inválido"); } if (item.DataNascimento >= DateTime.Now) { throw new Exception("Ano de Nascimento não pode ser maior que a data atual!"); } try { DPessoa.Atualizar(item); } catch { throw; } }
//PESQUISAR public static List <MPessoa> Pesquisar(MPessoa item) { List <MPessoa> retorno = null; if (item != null && item.Nome != null && item.Nome.Length <= 200) { string cpf = item.CPF.Replace(".", ""); cpf = cpf.Replace("-", ""); cpf = cpf.Trim(); if (cpf == "") { item.CPF = ""; } retorno = DPessoa.Pesquisar(item); //RETORNO SE NÃO HOUVER NENHUM CADASTRO if (retorno == null) { throw new Exception("A Pesquisa não retornou nenhum cadastro!"); } } return(retorno); }
//BOTÃO LIMPAR private void btnLimpar_Click(object sender, EventArgs e) { MPessoa item = new MPessoa(); item.CPF = txtCPF.Text = (""); item.Nome = txtNome.Text = (""); dataGridView1.DataSource = null; }
//FUNÇÃO PARA CARREGAR AS PESSOAS NO COMBOBOX private void carregarPessoa() { MPessoa item = new MPessoa(); item.Nome = ""; item.CPF = ""; cbNomeProprietarioCadastro.DataSource = CPessoa.Pesquisar(item); }
private void btnPesquisar_Click(object sender, EventArgs e) { MPessoa item = new MPessoa(); item.CPF = txtCPF.Text; item.Nome = txtNome.Text; dataGridView1.DataSource = CPessoa.Pesquisar(item); }
private void VVeiculoCadastro_Load(object sender, EventArgs e) { atual = CPessoa.Obter(atual); if (atual != null) { textBoxCPF.Text = atual.CPF; } else { Close(); } }
private void Excluir() { if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) { DialogResult r = MessageBox.Show("Deseja excluir esta pessoa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (r == DialogResult.Yes) { //forma 1 //MPessoa item = new MPessoa(); //item.CPF = dataGridView1.SelectedRows[0]. // Cells["cPFDataGridViewTextBoxColumn"].Value.ToString(); //forma 2 MPessoa item = (MPessoa)dataGridView1.SelectedRows[0]. DataBoundItem; bool sucesso = false; try { CPessoa.Excluir(item); sucesso = true; } catch { MessageBox.Show("Erro ao excluir a pessoa selecionada", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (sucesso) { MessageBox.Show("Pessoa excluída com sucesso", "", MessageBoxButtons.OK, MessageBoxIcon.Information); //Forma 1 //btnPesquisar_Click(null, null); //Forma 2 List <MPessoa> lista = (List <MPessoa>)dataGridView1.DataSource; lista.Remove(item); dataGridView1.DataSource = null; dataGridView1.DataSource = lista; } } } }
//INSERIR public static void Inserir(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "" + "INSERT INTO TBPessoa(CPF, Nome, DataNascimento) " + "VALUES(@CPF, @Nome, @DataNascimento)"; SqlParameter param = new SqlParameter("@CPF", SqlDbType.Char); param.Value = item.CPF; comando.Parameters.Add(param); param = new SqlParameter("@Nome", SqlDbType.VarChar); param.Value = item.Nome; comando.Parameters.Add(param); param = new SqlParameter("@DataNascimento", SqlDbType.Date); param.Value = item.DataNascimento; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw new Exception("CPF ja existente no sistema!"); } finally { conexao.Close(); } }
//ATUALIZAR public static void Atualizar(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "" + "UPDATE TBPessoa SET Nome = @Nome, DataNascimento = @DataNascimento " + " WHERE CPF = @CPF"; SqlParameter param = new SqlParameter("@CPF", SqlDbType.Char); param.Value = item.CPF; comando.Parameters.Add(param); param = new SqlParameter("@Nome", SqlDbType.VarChar); param.Value = item.Nome; comando.Parameters.Add(param); param = new SqlParameter("@DataNascimento", SqlDbType.Date); param.Value = item.DataNascimento; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw new Exception("O comando não pode ser executado"); } finally { conexao.Close(); } }
public static List <MPessoa> PesquisarComboBox(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "" + " SELECT CPF, Nome, DataNascimento " + " FROM TBPessoa " + " WHERE 1=1 "; SqlDataReader reader = comando.ExecuteReader(); List <MPessoa> retorno = null; while (reader.Read()) { if (retorno == null) { retorno = new List <MPessoa>(); } MPessoa pessoa = new MPessoa(); pessoa.CPF = reader["CPF"].ToString(); pessoa.Nome = reader["Nome"].ToString(); pessoa.DataNascimento = (DateTime)reader["DataNascimento"]; retorno.Add(pessoa); } reader.Close(); conexao.Close(); return(retorno); }
private void VPessoaEditar_Load(object sender, EventArgs e) { atual = CPessoa.Obter(atual); if (atual != null) { txtCPF.Text = atual.CPF; txtNome.Text = atual.Nome; txtDataNascimento.Value = atual.DataNascimento; } else { Close(); } }
//OBTER public static MPessoa Obter(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "" + " SELECT CPF, Nome, DataNascimento " + " FROM TBPessoa " + " WHERE CPF = @CPF "; SqlParameter param = new SqlParameter("@CPF", SqlDbType.Char); param.Value = item.CPF; comando.Parameters.Add(param); SqlDataReader reader = comando.ExecuteReader(); MPessoa retorno = null; if (reader.Read()) { retorno = new MPessoa(); retorno.CPF = reader["CPF"].ToString(); retorno.Nome = reader["Nome"].ToString(); retorno.DataNascimento = (DateTime)reader["DataNascimento"]; } reader.Close(); conexao.Close(); return(retorno); }
public static void Excluir(MPessoa item) { if (item.CPF.Trim() == "") { throw new Exception("CPF inválido"); } try { DPessoa.Excluir(item); } catch { throw; } }
//INSERIR public static void Inserir(MPessoa item) { if (item == null) { throw new Exception("Objeto PESSOA inválido"); } //NOME if (item.Nome.Trim() == "" || item.Nome.Length > 200) { throw new Exception("O Nome digitado esta inválido"); } //CPF if (item.CPF.Trim() == "" || item.CPF.Length != 14) { throw new Exception("Objeto CPF em branco, ou com numeros incompletos"); } for (int i = 0; i < 14; i++) { if (i != 3 && i != 7 && i != 11) { if (!Char.IsDigit(item.CPF[i])) { throw new Exception("CPF inválido"); } } } //DATA DE NASCIMENTO if (item.DataNascimento > DateTime.Now) { throw new Exception("Ano de Nascimento não pode ser maior que a data atual!"); } try { DPessoa.Inserir(item); } catch { throw; } }
//EDITAR PESSOA private void Editar() { if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) { MPessoa item = new MPessoa(); item.CPF = dataGridView1.SelectedRows[0]. Cells["cPFDataGridViewTextBoxColumn"].Value.ToString(); VPessoaEditar v = new VPessoaEditar(item); v.ShowDialog(); if (v.Atualizou) { btnPesquisar_Click(null, null); } } }
//BOTÃO PESQUISAR private void btnPesquisar_Click(object sender, EventArgs e) { MPessoa item = new MPessoa(); item.CPF = txtCPF.Text; item.Nome = txtNome.Text; dataGridView1.DataSource = null; try { dataGridView1.DataSource = CPessoa.Pesquisar(item); } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
private void button1_Click(object sender, EventArgs e) { //todo: validação da entrada MPessoa item = new MPessoa(); item.CPF = txtCPF.Text; item.Nome = txtNome.Text; item.DataNascimento = txtDataNascimento.Value; try { CPessoa.Inserir(item); MessageBox.Show("Dados salvos com sucesso!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static MPessoa Obter(MPessoa item) { MPessoa retorno = null; if (item != null) { string cpf = item.CPF.Replace(".", ""); cpf = cpf.Replace("-", ""); cpf = cpf.Trim(); if (cpf == "") { item.CPF = ""; } retorno = DPessoa.Obter(item); } return(retorno); }
public static void Atualizar(MPessoa item) { if (item == null) { throw new Exception("Objeto PESSOA inválido"); } if (item.Nome.Trim() == "" || item.Nome.Length > 200) { throw new Exception("Objeto PESSOA nome inválido"); } try { DPessoa.Atualizar(item); } catch { throw; } }
public static List <MPessoa> Pesquisar(MPessoa item) { List <MPessoa> retorno = null; if (item != null && item.Nome != null && item.Nome.Length <= 200) { string cpf = item.CPF.Replace(".", ""); cpf = cpf.Replace("-", ""); cpf = cpf.Trim(); if (cpf == "") { item.CPF = ""; } retorno = DPessoa.Pesquisar(item); } retorno = DPessoa.Pesquisar(item); return(retorno); }
//EXCLUIR public static void Excluir(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "DELETE FROM TBPessoa where CPF = @CPF"; SqlParameter param = new SqlParameter("@CPF", SqlDbType.Char); param.Value = item.CPF; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw; } finally { conexao.Close(); } }
//EXCLUIR PESSOA private void Excluir() { if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) { DialogResult r = MessageBox.Show("Deseja excluir esta pessoa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (r == DialogResult.Yes) { MPessoa item = (MPessoa)dataGridView1.SelectedRows[0]. DataBoundItem; bool sucesso = false; try { CPessoa.Excluir(item); sucesso = true; } catch { MessageBox.Show("Proprietário Tem Veiculo Cadastro! Gentileza Excluir o Veículo Primeiro", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (sucesso) { MessageBox.Show("Pessoa excluída com sucesso", "", MessageBoxButtons.OK, MessageBoxIcon.Information); List <MPessoa> lista = (List <MPessoa>)dataGridView1.DataSource; lista.Remove(item); dataGridView1.DataSource = null; dataGridView1.DataSource = lista; } } } }
//BOTÃO SALVAR private void button1_Click(object sender, EventArgs e) { MPessoa item = new MPessoa(); item.CPF = txtCPF.Text; item.Nome = txtNome.Text; item.DataNascimento = txtDataNascimento.Value; try { CPessoa.Inserir(item); MessageBox.Show("Dados salvos com sucesso!"); //LIMPA OS CAMPOS PARA NOVOS CADASTROS E PERMITE REALIZAR DIVERSOS CADASTRO NA MESMA TELA item.CPF = txtCPF.Text = (""); item.Nome = txtNome.Text = (""); item.DataNascimento = txtDataNascimento.Value = DateTime.Today; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static void Inserir(MPessoa item) { if (item == null) { throw new Exception("Objeto PESSOA inválido"); } if (item.Nome.Trim() == "" || item.Nome.Length > 200) { throw new Exception("Objeto PESSOA nome inválido"); } //todo CPF //todo DataNascimento try { DPessoa.Inserir(item); } catch { throw; } }
public VPessoaEditar(MPessoa item) { InitializeComponent(); atual = item; }
public VVeiculoCadastro(MPessoa item) { InitializeComponent(); atual = item; }
//PESQUISAR public static List <MPessoa> Pesquisar(MPessoa item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("Falha na conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "" + " SELECT CPF, Nome, DataNascimento " + " FROM TBPessoa " + " WHERE 1=1 "; if (item.CPF.Trim() != "") { comando.CommandText += " AND CPF = @CPF "; SqlParameter param = new SqlParameter("@CPF", SqlDbType.Char); param.Value = item.CPF; comando.Parameters.Add(param); } if (item.Nome.Trim() != "") { comando.CommandText += " AND Nome LIKE @Nome "; SqlParameter param = new SqlParameter("@Nome", SqlDbType.VarChar); param.Value = "%" + item.Nome + "%"; comando.Parameters.Add(param); } SqlDataReader reader = comando.ExecuteReader(); List <MPessoa> retorno = null; while (reader.Read()) { if (retorno == null) { retorno = new List <MPessoa>(); } MPessoa pessoa = new MPessoa(); pessoa.CPF = reader["CPF"].ToString(); pessoa.Nome = reader["Nome"].ToString(); pessoa.DataNascimento = (DateTime)reader["DataNascimento"]; retorno.Add(pessoa); } reader.Close(); conexao.Close(); return(retorno); }