Error: Incorrect decimal value: ' for column 'Value' at Row 1

Asked

Viewed 868 times

1

I’m having a problem inserting a die of a maskedTextBox in the Mysql database.

Follow the error

Incorrect decimal value: ' for column 'Value' at Row 1

The code is this:

              conexao.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conexao;

                cmd.CommandText = "insert into produto(ProdutoID, ProdutoDescricao, Valor) values (@ProdutoID, @ProdutoDescricao, @Valor)";
                cmd.Parameters.AddWithValue("ProdutoID", tb_CodigoProduto.Text.Trim());
                cmd.Parameters.AddWithValue("ProdutoDescricao", tb_DescricaoProduto.Text.Trim());
                cmd.Parameters.AddWithValue("Valor",mkd_ValorProduto.Text.Trim());

                int valorRetorno = cmd.ExecuteNonQuery();
  • mkd_ValorProduto is empty.

  • It’s not, I took the amount, but it’s in real for example, it’s 123.89

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

2 answers

1

I imagine the column Valor is a numeric type and therefore should be recorded as a number and not as text the way you are doing so you need to convert the data instead of recording directly. And you need to validate if it’s possible to talk, it would be something like this:

if (!decimal.TryParse(mkd_ValorProduto.Text, out var valor) {
    //tratar o erro aqui
}
cmd.Parameters.AddWithValue("Valor", valor);

I put in the Github for future reference.

You need to be in the right culture.

  • Thanks, I corrected the error! However, it is entering in the database with value 0.00

  • So you keep doing it wrong.

0

As the user Maniero said, you must convert its string value '100.5' for example, to a floating value to record in the bank. It is recording 0.00 because probably its conversion function is returning 0.00. Do a debug to know what the function return. Also check if you are trying to insert with a comma or dot.

Browser other questions tagged

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