Problem in SQL Query

0

I'm developing a C # application and I use a BD in MySql. In a part of the system, I need to increment the inventory of a particular product, test the command in the Workbench and it worked fine, but when I execute this command inside visual studio it returns error. Follow the code

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

//Query SQL
MySqlCommand command2 = new MySqlCommand("UPDATE estoque SET quantidade_estoque = quantidade_estoque + $'"+ txtQuantidade.Text +"' WHERE produto_estoque = '"+ cmbbProduto.Text +"')", mConn);

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

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

And the error is as follows:

  

MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL   syntax; check the manual that corresponds to your MySQL server version   for the right syntax to use near '' 500 'WHERE producto_estoque =   'Product') 'at line 1'

Any idea what it might be?

    
asked by anonymous 02.02.2018 / 14:04

2 answers

0

Good as I understand the field "quantity_to_stack" is a numeric and you are trying to increment by adding it to a string. ($ '"+ txtQuantity.Text +"') My suggestion is to change the query to the following:

MySqlCommand command2 = new MySqlCommand("UPDATE estoque SET quantidade_estoque = quantidade_estoque + " +  int.Parse(txtQuantidade.Text) + " WHERE produto_estoque = '" + cmbbProduto.Text + "')", mConn);
    
02.02.2018 / 14:29
0

After analyzing more closely, I identified the error and was closing the Query code. For future reference if someone has the same problem, the code that worked was:  // Open connection             mConn.Open ();

        //Query SQL
        MySqlCommand command2 = new MySqlCommand("UPDATE estoque SET quantidade_estoque = quantidade_estoque + " + int.Parse(txtQuantidade.Text) + " WHERE produto_estoque = '" + cmbbProduto.Text + "'", mConn);

        //Executa a Query SQL
        command2.ExecuteReader();
    
02.02.2018 / 16:03