How to Change a Data in the BD

Asked

Viewed 70 times

-2

I’m trying to change the data registered in the database, for example, when I click on DataGrid He pulls the dice for me textBoxs, but I wish I could save the changes made, but it does not change, appears to a MenssageBox saying that the register was changed, porem tanto no DataGrid how much in the BD is not being updated

code I’m using:

private void btnSalvar2_Click(object sender, EventArgs e)
    {            

            MySqlConnection conectar = new MySqlConnection("server=127.0.0.1;database=ProdPacote; Uid=root; pwd=1234;");
            conectar.Open();

            var transaction = conectar.BeginTransaction();

            MySqlCommand command = new MySqlCommand("UPDATE Produto SET Nome = @NOME, Descricao = @DESCRICAO, PRECO = @PRECO, STATUS = @STATUS WHERE ID_Produto = @ID", conectar);
            command.Parameters.AddWithValue("@Nome", txtNome.Text);
            command.Parameters.AddWithValue("@Descricao", txtDescricao.Text);
            command.Parameters.AddWithValue("@Preco", txtPreco.Text);
            command.Parameters.AddWithValue("@status", ativIndat);
            command.Parameters.AddWithValue("@ID", lblID);                

            command.ExecuteNonQuery();

            transaction.Commit();
            conectar.Close();
            MessageBox.Show("Produto alterado", "Concluido",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information);
            selecionarCategoria();
            btnSalvar.Visible = true;
            btnSalvar2.Visible = false;

    }

Note: I am using Mysql as a database, follow a photo of the code

CREATE DATABASE ProdPacote;
USE ProdPacote;

CREATE TABLE Produto(
ID_Produto INT PRIMARY KEY AUTO_INCREMENT,
Nome VARCHAR (200) NOT NULL,
Descricao VARCHAR (200) NOT NULL,
Preco DOUBLE NOT NULL,
`status` TINYINT NOT NULL);  

CREATE TABLE Pacote(
ID_Pacote INT PRIMARY KEY AUTO_INCREMENT,
Nome VARCHAR (200) NOT NULL,
peca VARCHAR (200) NOT NULL,
Quantidade INT NOT NULL,
Data_entrada DATETIME NOT NULL);
#Data_saida DATE NOT NULL




CREATE TABLE Produto_Pacote(
ID_Produto_Pacote INT PRIMARY KEY AUTO_INCREMENT,
Data_Hora DATE NOT NULL,
FK_ID_Produto INT NOT NULL,
FK_ID_Pacote INT NOT NULL,

CONSTRAINT Produto_Pacote
FOREIGN KEY (FK_ID_Produto)
REFERENCES Produto (ID_Produto),

CONSTRAINT Pacote_Produto
FOREIGN KEY(FK_ID_Pacote)
REFERENCES Pacote(ID_Pacote));
  • Pietro, try to put this SQL code in code written here in the publication, please.

  • It would be important to click [Dit] and reduce your post to a [mcve] only with what is necessary to reproduce the problem. The way it is depends on full analysis of your code, which is not suitable for the format of the site. Also, in normal situations. code should not be posted as image.

  • @Cypherpotato, how do I put the code in Mysql, kind of gets all messy if I use the Code

  • or how to call you in chat to send, or how to show it

  • @Pietronunciaroni only put four spaces before each line of code, the same as in the C#.

  • @Cypherpotato, I’ve updated the question, but I still can’t do the btnSalvar2 change the registered data

  • 1

    @Cypherpotato, Example: it appears the message that has changed, but not yet changed in the database

Show 2 more comments

1 answer

2


At the end of the string "UPDATE Produto SET Nome = @NOME, Descricao = @DESCRICAO, PRECO = @PRECO, STATUS = @STATUS WHERE ID = @ID" which is passed to the constructor MySqlCommand command = new MySqlCommand(... is written like this WHERE ID = @ID.

The parameter @ID was not defined in command. To resolve set the parameter '@ID':

command.Parameters.AddWithValue("@ID", Valor_do_ID);

where Valor_do_ID you replace by the source of that id.

Browser other questions tagged

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