public ProdutoHigiene ObterPeloId(int id) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = CadeiaDeConexao; conexao.Open(); SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = @"SELECT * FROM produtos_higiene WHERE id = @ID"; comando.Parameters.AddWithValue("@ID", id); DataTable tabela = new DataTable(); tabela.Load(comando.ExecuteReader()); if (tabela.Rows.Count == 1) { DataRow row = tabela.Rows[0]; ProdutoHigiene produto = new ProdutoHigiene(); produto.ID = Convert.ToInt32(row["id"]); produto.Nome = row["nome"].ToString(); produto.Preco = Convert.ToDecimal(row["preco"]); produto.Categoria = row["categoria"].ToString(); return(produto); } return(null); }
public List <ProdutoHigiene> ObterTodos() { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = CadeiaDeConexao; conexao.Open(); SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = @"SELECT * FROM produtos_higiene"; DataTable tabela = new DataTable(); tabela.Load(comando.ExecuteReader()); List <ProdutoHigiene> listaProdutos = new List <ProdutoHigiene>(); for (int i = 0; i < tabela.Rows.Count; i++) { DataRow row = tabela.Rows[i]; ProdutoHigiene produto = new ProdutoHigiene(); produto.ID = Convert.ToInt32(row["id"]); produto.Nome = row["nome"].ToString(); produto.Categoria = row["categoria"].ToString(); produto.Preco = Convert.ToDecimal(row["preco"]); listaProdutos.Add(produto); } return(listaProdutos); }
public AlterarProdutoHigiene(ProdutoHigiene produto) { InitializeComponent(); txtNome.Text = produto.Nome; cbCategoria.Text = produto.Categoria; txtPreco.Text = produto.Preco.ToString(); lblID.Text = produto.ID.ToString(); }
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value); RepositorioProdutosHigiene repositorio = new RepositorioProdutosHigiene(); ProdutoHigiene produto = repositorio.ObterPeloId(id); AlterarProdutoHigiene alterarProduto = new AlterarProdutoHigiene(produto); alterarProduto.ShowDialog(); }
private void AtualizarTabela() { RepositorioProdutosHigiene repositorio = new RepositorioProdutosHigiene(); List <ProdutoHigiene> listaProdutosHigiene = repositorio.ObterTodos(); dataGridView1.Rows.Clear(); for (int i = 0; i < listaProdutosHigiene.Count; i++) { ProdutoHigiene produto = listaProdutosHigiene[i]; dataGridView1.Rows.Add(new object[] { produto.ID, produto.Nome, produto.Categoria, produto.Preco }); } }
private void AlterarRegistro() { ProdutoHigiene produto = new ProdutoHigiene(); if (txtNome.Text.Length < 2) { MessageBox.Show("Registre o nome do produto"); txtNome.Focus(); return; } produto.Nome = txtNome.Text; if (cbCategoria.SelectedIndex == -1) { MessageBox.Show("Selecione a categoria"); cbCategoria.DroppedDown = true; return; } produto.Categoria = cbCategoria.Text; try { produto.Preco = Convert.ToDecimal(txtPreco.Text); if (produto.Preco < 0) { MessageBox.Show("O preço precisa ser maior que 0"); txtPreco.Focus(); return; } } catch (Exception) { MessageBox.Show("Somente números!"); txtPreco.Focus(); return; } produto.ID = Convert.ToInt32(lblID.Text); RepositorioProdutosHigiene repositorio = new RepositorioProdutosHigiene(); repositorio.Atualizar(produto); Close(); }
private void btnSalvar_Click(object sender, EventArgs e) { ProdutoHigiene produto = new ProdutoHigiene(); if (txtNome.Text.Length < 2) { MessageBox.Show("Registre o nome do produto"); txtNome.Focus(); return; } produto.Nome = txtNome.Text; if (cbCategoria.SelectedIndex == -1) { MessageBox.Show("Selecione a categoria"); cbCategoria.DroppedDown = true; return; } produto.Categoria = cbCategoria.Text; try { produto.Preco = Convert.ToDecimal(txtPreco.Text); if (produto.Preco < 0) { MessageBox.Show("O preço precisa ser maior que 0"); txtPreco.Focus(); return; } } catch (Exception) { MessageBox.Show("Somente números!"); txtPreco.Focus(); return; } RepositorioProdutosHigiene repositorio = new RepositorioProdutosHigiene(); repositorio.InserirRegistro(produto); Close(); }
public void InserirRegistro(ProdutoHigiene produto) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = CadeiaDeConexao; conexao.Open(); SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = @"INSERT INTO produtos_higiene (nome, categoria, preco) VALUES (@NOME, @CATEGORIA, @PRECO)"; comando.Parameters.AddWithValue("@NOME", produto.Nome); comando.Parameters.AddWithValue("@CATEGORIA", produto.Categoria); comando.Parameters.AddWithValue("@PRECO", produto.Preco); comando.ExecuteNonQuery(); conexao.Close(); }
public void Atualizar(ProdutoHigiene produto) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = CadeiaDeConexao; conexao.Open(); SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = @"UPDATE produtos_higiene SET nome = @NOME, preco = @PRECO, categoria = @CATEGORIA WHERE id = @ID"; comando.Parameters.AddWithValue("@NOME", produto.Nome); comando.Parameters.AddWithValue("@PRECO", produto.Preco); comando.Parameters.AddWithValue("@CATEGORIA", produto.Categoria); comando.Parameters.AddWithValue("@ID", produto.ID); comando.ExecuteNonQuery(); conexao.Close(); }
private void btnSalvar_Click(object sender, EventArgs e) { ProdutoHigiene produto = new ProdutoHigiene(); if (txtNome.Text.Length < 3) { MessageBox.Show("Por favor, registre o nome do produto Corretamente."); txtNome.Focus(); return; } produto.Nome = txtNome.Text; if (cbCategoria.SelectedIndex == -1) { MessageBox.Show("Selecione a categoria do produto"); cbCategoria.DroppedDown = true; return; } if (txtPreco.Text.Length < 1) { MessageBox.Show("Prencha o valor do produto"); txtPreco.Focus(); return; } try { produto.Preco = Convert.ToDecimal(txtPreco.Text); } catch (Exception) { MessageBox.Show("Somente números"); txtPreco.Focus(); return; } }