private void pbxBack_Click(object sender, EventArgs e) { CategoryAllForm category = new CategoryAllForm(aux); category.Show(); this.Close(); }
private void pbxSave_Click(object sender, EventArgs e) { SqlConnection sqlConnect = new SqlConnection(connectionString); //Save if (string.IsNullOrEmpty(lblId.Text)) { try { GetData(); Category c = new Category(name, active); sqlConnect.Open(); string sql = "INSERT INTO CATEGORY(NAME, ACTIVE) VALUES (@name, @active)"; SqlCommand cmd = new SqlCommand(sql, sqlConnect); cmd.Parameters.Add(new SqlParameter("@name", c.Name)); cmd.Parameters.Add(new SqlParameter("@active", c.Active)); cmd.ExecuteNonQuery(); MessageBox.Show("Adicionado com sucesso!"); Log.SaveLog(sqlConnect, "Categoria inserida", DateTime.Now, "Inserção"); CleanData(); } catch (Exception ex) { MessageBox.Show("Erro ao adicionar categoria!" + ex.Message); CleanData(); } finally { sqlConnect.Close(); } } //Edit else { try { GetData(); sqlConnect.Open(); string sql = "UPDATE CATEGORY(NAME, ACTIVE) VALUES (@name, @active) WHERE ID = @id"; SqlCommand cmd = new SqlCommand(sql, sqlConnect); cmd.Parameters.Add(new SqlParameter("@name", name)); cmd.Parameters.Add(new SqlParameter("@active", active)); cmd.Parameters.Add(new SqlParameter("@id", lblId.Text)); cmd.ExecuteNonQuery(); MessageBox.Show("Altereções salvas com sucesso!"); Log.SaveLog(sqlConnect, "Categoria editada", DateTime.Now, "Edição"); } catch (Exception Ex) { MessageBox.Show("Erro ao editar esta categoria!" + "\n\n" + Ex.Message); throw; } finally { sqlConnect.Close(); CategoryAllForm category = new CategoryAllForm(aux); category.Show(); this.Close(); } } }