Field write float to sql server with c#

Asked

Viewed 140 times

-2

Good morning, I’m doing an update in a float field on sql server, but I’m not getting this from a conversion error, I tried several ways and did not succeed, I keep getting this error return = Error Converting data type varchar to float.

follows my code.

            SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "update tbl_Produto set Lote = '" + cb_lote.Text + "', Qtda = CONVERT(FLOAT, '" + txt_qtda.Text + "') where  Cod_Produto = '" + txt_codigo.Text + "'";
            cmd.Connection = conex1;

            conex1.Open();
            cmd.ExecuteNonQuery();
            conex1.Close();
  • Where is making such a mistake ?

  • am receiving this error Error Converting data type varchar to float. when I update this field Qtda = CONVERT(FLOAT, '" + txt_qtda.Text + "') in my query. The error appears in the grinding of the execution of the cmd.Executenonquery query();

  • Are you sure that usurá float for this? https://answall.com/a/38140/101 And if the user enters something wrong, the application breaks?

  • Good is probably here Qtda = CONVERT(FLOAT, '" + txt_qtda.Text + "')

  • what has in txt_qtda.Text? Maybe the problem is formatting: select CONVERT(float, '0.123') works. (checks if the value is not comma)

  • But a quantity field in kg then has to be this way.

  • txt_qtda.Text values me Kilograms. Ex. 0,200 or 0,05 values like this

  • That’s right the value and put comma in the field.

  • I used a replace to replace the comma by point and it worked, but the amount that was supposed to be 0,200 he recorded 200 in the bank.

  • 1

    in this sql Injection code

  • Okay problem solved was putting the comma. I switched for point and it worked, recorded it right, at the time of entering the value, I typed with point and he recorded it right in the bank. .kkk. But thank you very much for all your attention.

  • Working is different than being right.

  • So I agree yes, now I just need to make the field only accept point and not accept comma, how can I do that?

Show 8 more comments

1 answer

0


Try to pass a parameter, something like this:

string sqlQuery = "update tbl_Produto set Lote=@lote, Qtda=@qtda where Cod_Produto=@codigo";

SqlCommand cmd = new SqlCommand(sqlQuery, conex1);

cmd.Parameters.AddWithValue("@lote", txt_qtda.Text);

cmd.Parameters.Add("@qtda", SqlDbType.Float);
cmd.Parameters["@qtda"].Value = (float) Convert.ToDouble(cb_lote.Text);

cmd.Parameters.Add("@codigo", SqlDbType.Int);
cmd.Parameters["@codigo"].Value = int.Parse(cb_lote.Text);

conex1.Open();
Int32 linhasResultado = cmd.ExecuteNonQuery();
  • This is a good idea, but the same problem is others, or others, have several in this code and they are still present in your, which introduces new errors.

  • Thanks so much for your help.

Browser other questions tagged

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