private void salvarAlter(object sender, EventArgs e)
        {
            string    nome    = Convert.ToString(dgCategorias.SelectedRows[0].Cells[1].Value);
            categoria alterar = new categoria();

            alterar      = bd.categoria.Where(u => u.nome.Equals(nome)).FirstOrDefault();
            alterar.nome = txtNome.Text;
            bd.SaveChanges();
            MessageBox.Show("Salvo com sucesso!");
            carregaTabela();
            btnSalvarAlteracao.Visible = false;
        }
 private void salvar(object sender, EventArgs e)
 {
     existe = bd.categoria.Where(u => u.nome.Equals(txtNome.Text)).FirstOrDefault();
     if (existe == null)
     {
         categoria nova = new categoria()
         {
             nome = txtNome.Text
         };
         bd.categoria.Add(nova);
         bd.SaveChanges();
         MessageBox.Show("Categoria cadastrada com sucesso!");
     }
     else
     {
         MessageBox.Show("Categoria já existe!");
     }
     carregaTabela();
 }
Exemplo n.º 3
0
        private void BtnSalvarAlteracao_Click(object sender, EventArgs e)
        {
            int       idProduto   = Convert.ToInt32(dgProdutos.SelectedRows[0].Cells[0].Value);
            categoria selecionada = categorias.ElementAt(cbxCategoria.SelectedIndex);
            produto   alteracao   = new produto();

            if (String.IsNullOrWhiteSpace(txtNome.Text) && String.IsNullOrWhiteSpace(txtValor.Text))
            {
                MessageBox.Show("Não houve alteração!");
            }
            else
            {
                alteracao              = bd.produto.Single(c => c.id.Equals(idProduto));
                alteracao.nome         = txtNome.Text;
                alteracao.descricao    = txtDescricao.Text;
                alteracao.id_categoria = selecionada.id;
                alteracao.preco        = Convert.ToDecimal(txtValor.Text);
                bd.SaveChanges();
                montarTabela();
                txtNome.Clear();
                txtValor.Clear();
                txtDescricao.Clear();
                bd.SaveChanges();
                clbIngredientes.Items.Clear();
                carregaIngredientes();
                ingredientesUsados.ForEach(u =>
                {
                    produto_ingrediente novoProdIng = new produto_ingrediente()
                    {
                        id_produto     = idProduto,
                        id_ingrediente = u.id
                    };
                    bd.produto_ingrediente.Add(novoProdIng);
                    bd.SaveChanges();
                });
            }
            btnSalvarAlteracao.Visible = false;
        }
Exemplo n.º 4
0
        private void salvarCadastroProduto(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtNome.Text) || String.IsNullOrWhiteSpace(txtValor.Text))
            {
                MessageBox.Show("Preencha os campos!");
            }
            else
            {
                nomeUsado = bd.produto.Where(c => c.nome.Equals(txtNome.Text)).FirstOrDefault();
                if (nomeUsado == null)
                {
                    categoria selecionada = categorias.ElementAt(cbxCategoria.SelectedIndex);
                    produto   novo        = new produto()
                    {
                        nome         = txtNome.Text,
                        descricao    = txtDescricao.Text,
                        preco        = Convert.ToDecimal(txtValor.Text),
                        id_categoria = selecionada.id,
                        imagem       = caminhoFoto
                    };

                    bd.produto.Add(novo);
                    bd.SaveChanges();
                    montarTabela();
                    produto nomeProd = bd.produto.Where(u => u.nome.Equals(txtNome.Text)).FirstOrDefault();
                    ingredientesUsados.ForEach(u =>
                    {
                        produto_ingrediente novoProdIng = new produto_ingrediente()
                        {
                            id_produto     = nomeProd.id,
                            id_ingrediente = u.id
                        };
                        bd.produto_ingrediente.Add(novoProdIng);
                        bd.SaveChanges();
                    });
                    //bd.produto.ToList().ForEach(f =>
                    //{
                    //    if (txtNome.Text == f.nome)
                    //    {

                    //        ingredientesUsados.ForEach(u =>
                    //        {
                    //            produto_ingrediente novoProdIng = new produto_ingrediente()
                    //            {
                    //                id_produto = f.id,
                    //                id_ingrediente = u.id
                    //            };
                    //            bd.produto_ingrediente.Add(novoProdIng);
                    //        });
                    //    }
                    //});
                    txtNome.Clear();
                    txtValor.Clear();
                    txtDescricao.Clear();
                    bd.SaveChanges();
                    clbIngredientes.Items.Clear();
                    carregaIngredientes();
                }
                else
                {
                    MessageBox.Show("Produto ja cadastrado!");
                }
            }
        }