private void pbxBack_Click(object sender, EventArgs e) { ProductAllForm paf = new ProductAllForm(userAux); paf.Show(); this.Hide(); }
private void pbxSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(lblId.Text)) { SqlConnection sqlConnect = new SqlConnection(connectionString); try { GetData(); Category c = (Category)cmbCategory.SelectedItem; Product p = new Product(name, price, c, active); sqlConnect.Open(); string sql = "INSERT INTO PRODUCT(NAME, PRICE, ACTIVE, FK_PRODUCT) VALUES (@name, @price, @active, @category)"; SqlCommand cmd = new SqlCommand(sql, sqlConnect); cmd.Parameters.Add(new SqlParameter("@name", p.Name)); cmd.Parameters.Add(new SqlParameter("@price", p.Price)); cmd.Parameters.Add(new SqlParameter("@active", p.Active)); cmd.Parameters.Add(new SqlParameter("@category", p.Category.Id)); cmd.ExecuteNonQuery(); MessageBox.Show("Adicionado com sucesso!"); Log.SaveLog(sqlConnect, "Produto Adicionado", DateTime.Now, "Adição"); } catch (Exception ex) { MessageBox.Show("Erro ao adicionar produto!" + ex.Message); ClearData(); } finally { ClearData(); sqlConnect.Close(); } } else { SqlConnection sqlConnect = new SqlConnection(connectionString); try { GetData(); Category c = (Category)cmbCategory.SelectedItem; sqlConnect.Open(); string sql = "UPDATE PRODUCT SET NAME = @name, PRICE = @price, ACTIVE = @active, FK_PRODUCT = @category WHERE ID= @id"; SqlCommand cmd = new SqlCommand(sql, sqlConnect); cmd.Parameters.Add(new SqlParameter("@id", lblId.Text)); cmd.Parameters.Add(new SqlParameter("@name", name)); cmd.Parameters.Add(new SqlParameter("@price", price)); cmd.Parameters.Add(new SqlParameter("@active", active)); cmd.Parameters.Add(new SqlParameter("@category", c.Id)); cmd.ExecuteNonQuery(); MessageBox.Show("Altereções salvas com sucesso!"); Log.SaveLog(sqlConnect, "Produto Adicionado", DateTime.Now, "Edição"); } catch (Exception Ex) { MessageBox.Show("Erro ao editar este produto!" + "\n\n" + Ex.Message); throw; } finally { sqlConnect.Close(); ProductAllForm paf = new ProductAllForm(userAux); paf.Show(); this.Close(); } } }