Changing Mysql data with C#

Asked

Viewed 143 times

-1

I am trying to change data saved in my BD, however by having a certain error, if someone can give a light

problema

code I’m using to save the changes:

private void btnSalvar_Click(object sender, EventArgs e)
     {
 conectar.Close();
            conectar.Open();
            //Convertendo

            Converter = Convert.ToInt32(txtQuantidade.Text);



            MySqlCommand Inserir = new MySqlCommand();
            Inserir.Connection = conectar;
            Inserir.CommandText = "UPDATE  Produto SET Nome = '" + txtNome.Text + 
                "', Quantidade = '" + txtQuantidade.Text + "', peca =" + cbxPeca.Text + 
                ", Data_entrada = " + DTPEntrada.Text + " WHERE ID_Pacote =" + alterar2 +  " ";

            Inserir.ExecuteNonQuery();
            conectar.Close();
            alterar2 = 0;
            txtNome.Text = "";
            txtQuantidade.Text = "";
            //txtDescricao.Text = "";
            MessageBox.Show("Pacote alterado", "Concluido",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information);
            selecionarCategoria();


        }

and here is how I pull the information to the textboxs to edit them:

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        alterar2 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
        txtNome.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        cbxPeca.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        txtQuantidade.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        DTPEntrada.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();

    }

database 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);





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));
  • In this other Post I was able to solve the error with the help of some members

1 answer

0

I also have a program that I have to change the data of the database and use this code (You have to put it in the change button) :

 foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {
                DataRow row = ((DataRowView)item.DataBoundItem).Row;
                Database.DatabaseAPI.NonQuery("UPDATE NomeDaTabela SET NomeDaColuna = '" + textBox1.Text + "' WHERE NomeDaColuna  = '" + row["NomeDaColuna "].ToString() + "'");
                MessageBox.Show("Dados Alterados com sucesso!");
                textBox1.Text = string.Empty;

            }

"Database.Databasepi" is the name I gave to make the database connections, try it and see if you can use.

  • then is that my code is inside a if I ended up putting only the part of else which is when it changes, but still gives the error, I will put the bank code

  • Also check that the database has the name of the column well written, or even there in the code.

  • then I put, which is strange because I went to the bank gave a CTRL+V and CTRL+C to take the table of the bank

  • I managed to figure out the problem in the update Voce has "UPDATE PRODUCT" and is not Product is Package change and see if it got.

  • worked, but in the cbxpeca of this error: Mysql.Data.Mysqlclient.Mysqlexception: 'Unknown column 'teste2' in 'field list''

  • try : peca = '" + cbxPeca.Text + "' (I think a single quotation mark was missing, because as it is a textbox you have to quote and double quote)

  • but there the Data_entrada make a mistake

  • What are you using to input date ? could be using a datetimepicker that it would get a lot easier, but that’s in my opinion.

  • In case you already have one datetimepicker try to enter this code before inserting: string data = "CONVERT(VARCHAR, '" + dateTimePicker1.Value.Date.Tostring("yyyy-MM-dd HH:mm:ss") + "', 103)"; , so before you type: Data_input = " + Dtpentrada.Text + " exchanges Dtpentrada.Text by date.

  • I got it, it’s him I’m wearing

  • try writing the code I put on top to see if it works

  • did not turn, he does not command this format, I wanted to get only the date

  • Well I returned the code with the first fixing error, if I can solve I put the solution here

  • If you only want the date and not the hours just take the part of the format (HH:mm:ss), because it is this part that defines the hours.

  • then I took from the DataTimePicke and the time is not sent to the bank, because when registering it does not register with hours

  • Now I don’t understand what I should do.

Show 11 more comments

Browser other questions tagged

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