Problem in Query SQL

Asked

Viewed 45 times

0

I am developing a C# application and I use a BD in Mysql. In a part of the system, I need to increase the stock of a certain product, test the command in Workbench and worked well, but when I run this command inside the visual studio returns me error. Follows 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 mistake is this:

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 product_stock = 'Product')' at line 1'

Any idea what it might be?

  • checked the data types? produto_estoque can be an id and is passing a text..

2 answers

0

Good as I understand the field "quantity_stock" is a numeric and you are trying to increment it by adding it to a string. ($'"+ txtQuantidity.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);
  • So, I had already tried to pass this way but returns the same error, with the difference that apparently Visual Studio does not recognize the value without the '$'.

  • Have you tried debugging to see what you are really trying to upload to the database.?

0

After analyzing more carefully I identified the error and was at the closing of the Query code. For future references if someone has the same problem, the code that worked was: // Open the 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();

Browser other questions tagged

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