Insert MYSQL error by data grid view

Asked

Viewed 52 times

-1

the code is as follows, I am making an Insert from values of a data grid view

                foreach (DataGridViewRow row in dgvCON.Rows)
                {
                        MySqlCommand cmd = new MySqlCommand();
                        cmd = conectabanco.CreateCommand();
                        if (row.IsNewRow) continue;
                        cmd.Parameters.AddWithValue("@IdProd", row.Cells["Id"].Value);
                        cmd.Parameters.AddWithValue("@QtdProd", row.Cells["Quantidade"].Value);
                        cmd.Parameters.AddWithValue("@NomeProd", row.Cells["Nome"].Value);
                        cmd.Parameters.AddWithValue("@PrecoUnitProd", row.Cells["R$ Unidade"].Value);
                        cmd.Parameters.AddWithValue("@TotalListaConsu", row.Cells["Total"].Value);
                        cmd.CommandText = "INSERT INTO listaconsumo(IdCon, IdProd, QtdProd, PrecoUnitProd, " +
                        "totalListaConsu)VALUES("+idconsumo+", @IdProd, @QtdProd, @NomeProd, @PrecoUnitProd, @totalListaConsu)";

                    cmd.ExecuteNonQuery();
                }

The problem is that when running Sql, it sends the "@Idprod" value itself instead of being replaced by the value of datagridview

  • 1

    Please [Edit] the question and add the code directly into the body of it. [en.so] has native support for the codes, so avoid posting them as an image. Do the [tour] to learn the basics of how the site works.

  • Thank you, already changed!

  • why not put this "+idconsumo+" as parameter too ?

1 answer

0


Before the amount of VALUES is different from the number of columns in the INSERT, you are not informing the column to the field NomeProd. And where do you pick up row.Cells["R$ Unidade"].Value I believe this is an invalid name for a datagridview column.

Check these questions and give feedback here please

The use to access the value of the line cell, is row.Cells[0].Value where 0 is the index of the column, or row.Cells["key"].Value where key is the name of the object DataGridViewColumn and not the name that appears on the screen. You can access directly by the object DataGridviewColumn informing the property Name. Ex:

row.Cells[nomeDaColuna.Name].Value

Another thing, in the question you said that the @Idprod field was not working, now it’s passed that @Nomeprod is not. Which is right ?

Try it there.

  • I arranged the difference in the amount of the values to be equal to the Insert, already the name "R$ Unit" is the same on the screen, so I can use it with this name I believe. But somehow the problem persists, it does not assign the datagridview information to the "@Nomeprod" command. I know I can get around the problem by inserting directly, without adding command parameter, but thank you

  • I did what I said, the error still persists. O erro "@NomeProd" ou "@IDProd" é o mesmo, em vez de ele substituir os valores da tela para enviar pro banco, ele envia da seguinte forma:
INSERT INTO listaconsumo(IdCon, IdProd, QtdProd, NomeProd, PrecoUnitProd, totalListaConsu)VALUES(20, "@IdProd", "@QtdProd"," @NomeProd", "@PrecoUnitProd", "@totalListaConsu")

Em vez de: 
INSERT INTO listaconsumo(IdCon, IdProd, NomeProd, QtdProd, PrecoUnitProd, totalListaConsu)VALUES(20, 2, 5, 7, 2, 5);

Até mesmo substitutindo pelo índice direto(Row.Cells[0]) still persists.

  • it is giving error at the time of the SQL command ? where are you seeing that it is passing the command this way ? it runs the command successfully and you see these stored values ?

  • Yes, in the SQL command the fields "@Prodname", "@Idprod", etc.... go without datagridview values, it cannot add the datagrid view value to the "@Prodname" extension for example.

  • If you are giving error in the SQL command, which error? If you are not giving error, you see these values @NomeProd stored inside the bank ?

  • I don’t get to see these values because it just doesn’t do this "@Nomeprod" assimilation command it just sends that name than the value that’s on the screen

  • That’s my question, where are you seeing that the command is not going with the values?

  • When I break a point in the line "Execute non query" and see the Insert, in case the text it is sending to the bank, that would be it?

  • is just like that, Mysqlcommand will not replace the values of the parameters in the query, it will run with these parameters, qm will do this is the communication driver, in the case of Mysql. you can’t see it at runtime by putting a break point

  • I understand, because when I use it directly ( instead of creating commands and parameters game each Row direct in the Insert ) I can see the values in the break point of the code. Anyway, thank you very much!

  • Although it is more practical not to use the parameters, it is a risk because it allows SQL Injection, as well as possible syntax problems when inserting a text with quotes for example. I recommend using the parameters.

Show 6 more comments

Browser other questions tagged

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