Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When click on update command button the error is

"Conversion from string "Syntax error in UPDATE statement" to type 'Integer' is not valid."

VB
        BtnSave.Enabled = True
        BtnAdd.Enabled = True
        BtnUpdate.Enabled = True
        BtnDelete.Enabled = True
        BtnPrevious.Enabled = True
        BtnNext.Enabled = True
        BtnFirst.Enabled = True
        BtnLast.Enabled = True

        Try

            Dim cmd As New OleDb.OleDbCommand()
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= E:\VB Project\Tansportation\Transport2003.mdb"



            con.Open()

            
            cmd.Connection = con

            cmd.CommandText = "UPDATE TblBillStatmentEntry SET Date='" & TxtDate.Text & "',FreightSlipNo='" & TxtFreightSlipNo.Text & "',FromID='" & ComboBox1.Text & "',To='" & TxtTo.Text & "',RatesPerTon='" & TxtRatePerTon.Text & "',RatesPerTrip='" & TxtRatePerTrip.Text & "',WorkEffectiveDate='" & TxtWEF.Text & "' ,MaterialID='" & ComboBox2.Text & "', TruckNoID='" & ComboBox3.Text & "',TonCategory='" & TxtTon.Text & "',ConditionListID='" & ComboBox4.Text & "',Weight='" & TxtExtra.Text & "',Trips='" & TxtTrips.Text & "',ActualWeight='" & TxtActualWeight.Text & "',BillWeight='" & TxtBillWeight.Text & "',Amount='" & TxtAmount.Text & "' WHERE ID='" & TxtID.Text & "' "


           
cmd.ExecuteNonQuery()
            If con.State = ConnectionState.Open Then
                con.Close()
            End If

            MsgBox("Data Updated")
        Catch ex As Exception
            MsgBox("Cant load", ex.Message)

        End Try

       
    End Sub
Posted
Updated 15-Apr-15 22:15pm
v2
Comments
Afzaal Ahmad Zeeshan 16-Apr-15 4:14am    
Time to learn basics of English language before learning programming langauge. Conversion from string "Syntax error in UPDATE statement" to type 'Integer' is not valid. It tells that your object is a string, and cannot convert it to Integer because it includes non-digit characters.

1 solution

The immediate problem is as Afzaal said: the value you give for one or more columns is not convertible to an integer, which the column expects.

But you have bigger problems than that!

You can't assume that users will enter valid data - so rather than assuming they will and sending it to SQL regardless and letting it sort it out, you really should check and convert each numeric value, each date based value, and report individually on the problems so the user can fix them.
Since you have a lot of inputs, just telling him "Cant load" isn't at all helpful - and users will spend a lot more time trying to work out which data is wrong than you would spend adding the code to check it!
It's not complex:
VB
Dim textBox As New TextBox()
Dim value As Integer
If Not Integer.TryParse(textBox.Text, value) Then
        ' Report problem
        ...
End If


Secondly, don't do SQL like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, and pass the checked and converted values directly as the right datatype.

Thirdly, if this really is VB.NET, then don't use MsgBox - for starters, you should be using the .NET MessageBox.Show instead, and secondly VB code runs on the server, not the client - so the message will show at the server, and the client will not get any response...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900