Making the insert in SQL Server in a float field?

4

I have the following problem in my code, I have a textbox that receives the value 0.900 , but when I am writing this value to the bank, it is only recording 900, and I need to record the 0.900 and I can not write this way, the field in the sql table is as float . I'm trying to do float.Parse(txt_qtda.Text) and it's not working, just write 900 and not 0.900 .

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO CB0020 (CB0_FILIAL, CB0_CODETI, CB0_DTNASC, CB0_TIPO, CB0_CODPRO, CB0_QTDE, CB0_LOCAL, CB0_LOTE, CB0_DTVLD, CB0_FORNEC, CB0_LOJAFO, CB0_XLOTEF, CB0_XQTDKG, CB0_XORIGE, CB0_XDTFAB, CB0020.R_E_C_N_O_) VALUES ('01','" + txt_codbarras.Text + "', '" + txt_dtnasc.Text + "', '01', '" + txt_codprod.Text + "', '" + float.Parse(txt_qtda.Text) + "', '01', '" + txt_lotefrac.Text + "', '" + Convert.ToDateTime(txt_dtvalidfrac.Text).ToString("yyyy/MM/dd").Replace("/", "") + "', '" + txt_codfbaric.Text + "', '01', '" + txt_lotefabric.Text + "', '" + float.Parse(txt_qtda.Text) + "', '" + txt_codorig.Text + "', '" + Convert.ToDateTime(txt_dtfabricfrac.Text).ToString("yyyy/MM/dd").Replace("/", "") + "', '" + txt_recno.Text + "')";
cmd.Connection = conex;
conex.Open();
cmd.ExecuteNonQuery();
    
asked by anonymous 08.08.2017 / 15:00

1 answer

4

There are several errors there.

This looks like money or something similar (quantity) and float should never be used for this. This is a very serious problem that causes damage.

If there is a wrong typing the application will break . The problem should actually be in the conversion or input of the data.

If the conversion is correct it is for everything to work. But if you still have some reason to change the scale just divide by 1000. Not that I am saying that is the case, I did not see the whole, but I have seen situation that the solution would be this. Do not use this as a gambiarra, only if it is the correct solution. It is a useful solution when you really enter the value on a scale different from the one you want. If it is to fix the previous error, do not.

You have serious security issues by entering data in this way. Parametrize the query .

These date conversions look strange too.

The nomenclature of the columns and the table is usually bad, but this has a little taste, although almost everyone would agree that these names are illegible.

    
08.08.2017 / 15:09