Error inserting data in table with column of the numeric type VB NET

Asked

Viewed 53 times

0

Hello, I am having problems when trying to insert data in an SQL database through VB.NET.

I’m migrating some vb6 projects to . net, I came across the first problem I spent the day trying to solve unsuccessfully.

sql = "INSERT INTO Abastecimento (Data, Data_abast, hora, prefixo, bomba, Combustivel) VALUES ('" & Trim(TextBox1.Text) & "','" & Trim(TextBox2.Text) & "','" &
                                    dd & "','" & Trim(TextBox5.Text) & "','" & Trim(CInt(TextBox12.Text)) & "','" & v_combustivel & "')"

When trying to do this INSERT, I get the error:

Error Converting data type

I found that when removing the column Fuel the query works, or is something wrong when trying to insert in this column.

Obs:
1 - The Fuel column is as Numeric(4, 1) in sql server
2 - The value I am trying to pass through the variable v_fuel is 150.5; I have tried to replace comma by dot etc.

If anyone can help, thank you :D

  • This must be: https://answall.com/q/104130/101 And this may help: https://answall.com/q/293843/101, so the way you’re doing it is completely wrong.

  • Already tried printing for console or for MessageBox the result of the variable sql? After placing this result directly in SQL I was sure that already understood what the problem was. But as @Maniero said, it’s not the right way to do it.

  • INSERT INTO Refuelling(Date, Data_abast, time, prefix, pump, Fuel) VALUES ('2019/10/08','2019/10/0/','0:30:38','000431','1','25,1') Note:"Fuel is in SQL table as Numeric(4, 1)" but I haven’t been able to identify the cause of the problem. I did the same way on VB6 with cdbl() worked well. : / Note:Regarding being the correct way to do for security reasons etc, I am still studying so much there was on the basis of trial and error. Thank you !

  • Solution: Instead of joining two textbox into a variable, I asked for the integer value only in a variable, in the case of textbox1, I replaced() to replace the comma by a dot and it worked well. sql = "INSERT INTO Refuelling (Date, Data_abast, time, prefix, pump, Fuel) VALUES ('" & Trim(data_SQL(Textbox1.Text)) & "','" & Trim(data_SQL(Textbox2.Text)) & "','" dd & "','" & Trim(Textbox5.Text) & "','" & Trim(Cint(Textbox12.Text)) & "'," & Trim(Textbox1.Text) & ")"

No answers

Browser other questions tagged

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