Пример #1
0
        private void pbxSave_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnect = new SqlConnection(connectionString);

            //Save
            if (string.IsNullOrEmpty(lblId.Text))
            {
                try
                {
                    GetData();

                    Category c = (Category)cmbCategory.SelectedItem;
                    Product  p = new Product(name, active, c, price);

                    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 inserido", DateTime.Now, "Inserção");
                    CleanData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro ao adicionar categoria!" + ex.Message);
                    CleanData();
                }
                finally
                {
                    sqlConnect.Close();
                }
            }
            //Edit
            else
            {
                try
                {
                    GetData();

                    Category c = (Category)cmbCategory.SelectedItem;

                    sqlConnect.Open();
                    string sql = "UPDATE PRODUCT(NAME, PRICE, ACTIVE, FK_PRODUCT) VALUES (@name, @price, @active, @category) WHERE ID = @id";

                    SqlCommand cmd = new SqlCommand(sql, sqlConnect);

                    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.Parameters.Add(new SqlParameter("@id", lblId.Text));
                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Altereções salvas com sucesso!");
                    Log.SaveLog(sqlConnect, "Produto editado", DateTime.Now, "Edição");
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("Erro ao editar este produto!" + "\n\n" + Ex.Message);
                    throw;
                }
                finally
                {
                    sqlConnect.Close();

                    ProductAllForm product = new ProductAllForm(aux);
                    product.Show();
                    this.Close();
                }
            }
        }