How To Make My Save, Change My Database Data

Asked

Viewed 36 times

-1

Guys, I’m trying to make a Button that normally Salva, also become a Button that saves the changes made... It’s all the same form and the same Button, for example when I click two DataGrid he brings to me TextBox the information, however if I edit them it registers as a new record, and I wanted to save the changes made to the selected record, if someone can give a light, thank you

Code I’m using below

 //criando a bool
        bool informacoesSalvas = false;

        private void btnSalvar_Click(object sender, EventArgs e)
        {

        if (string.IsNullOrWhiteSpace(txtNome.Text))
        {
            System.Windows.Forms.MessageBox.Show("O nome do produto está vazio, por favor digite algo");
            return; // sai do método
        }
        else if (string.IsNullOrWhiteSpace(txtPreco.Text))
        {
            System.Windows.Forms.MessageBox.Show("O Preço do produto está vazio, por favor digite algo");
            return;
        }

        else if (informacoesSalvas)
        {

            // Abre a conexão
            conectar.Open();

            //Query SQL
            MySqlCommand command = new MySqlCommand("INSERT INTO Produto (Nome,Descricao,Preco,status)" +
            "VALUES('" + txtNome + "','" + txtDescricao.Text + "','" + txtPreco.Text + "'," + ckbAtiv.Checked + " )", conectar);

            //Executa a Query SQL
            command.ExecuteNonQuery();

            // Fecha a conexão
            conectar.Close();

            //Mensagem de Sucesso
            MessageBox.Show("Alterado com Sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);

            informacoesSalvas = true;
        }
        else
        {

            conectar.Open();
            //Convertendo

            Converter = Convert.ToDecimal(txtPreco.Text);

            //MessageBox.Show("Conectado");

            MySqlCommand Inserir = new MySqlCommand();
            Inserir.Connection = conectar;
            Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco, `status`) VALUES (@peca, @nome, @quantidade, @dataentrada)";

            Inserir.Parameters.AddWithValue("@peca", txtNome.Text);
            Inserir.Parameters.AddWithValue("@nome", txtDescricao.Text);
            Inserir.Parameters.AddWithValue("@quantidade", txtPreco.Text);
            Inserir.Parameters.AddWithValue("@dataentrada", ckbAtiv.Checked);

            Inserir.ExecuteNonQuery();
            conectar.Close();
            MessageBox.Show("Produto Cadastrado", "Concluido",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information);
            selecionarCategoria();
        }
    }

1 answer

2


Note that in your SQL query both parts of if do the Insert operation, which in your case would be an update and an Insert.

UPDATE Produto SET Nome = @NOME, Descricao = @DESCRICAO, PRECO = @PRECO, STATUS = @STATUS WHERE ID = @ID

Note that when doing the UPDATE i pass an ID as a condition in WHERE, this so that only the desired record is changed. Tip: never forget the WHERE

  • that I would have to do in the bank or in the C# itself ?

  • made it into another Button to see if it would look better but apparently is making another mistake, if you can take a look at it link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.