简体   繁体   中英

Unable to Update MS-Access Database table using VB.net

i am trying to update MS-Access database table with the code below using VB.net and i get this Error "Syntax error in UPDATE statement"

Dim Dcon As OleDbConnection
Dim Dcom As OleDbCommand
Dcon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DataSource & ";")

Dcom = New OleDbCommand("UPDATE Drivers SET ID=?,First=?,Last=?,Company=?,Addr=?,City=?,ST=?,Zip=?,MobileP=?,HomeP=?,Email=?,DL=?,DateSince=?,DateTerm=?,TruckID=?,Commants=?,Image=? WHERE ID = ID=?", Dcon)
Dcom.Parameters.AddWithValue("@ID", Label3.Text)
Dcom.Parameters.AddWithValue("@First", TextBox1.Text)
Dcom.Parameters.AddWithValue("@Last", TextBox2.Text)
Dcom.Parameters.AddWithValue("@Company", TextBox3.Text)
Dcom.Parameters.AddWithValue("@Addr", TextBox4.Text)
Dcom.Parameters.AddWithValue("@City", TextBox5.Text)
Dcom.Parameters.AddWithValue("@ST", TextBox6.Text)
Dcom.Parameters.AddWithValue("@Zip", TextBox7.Text)
Dcom.Parameters.AddWithValue("@MobileP", TextBox8.Text)
Dcom.Parameters.AddWithValue("@HomeP", TextBox9.Text)
Dcom.Parameters.AddWithValue("@Email", TextBox10.Text)
Dcom.Parameters.AddWithValue("@DL", TextBox11.Text)
Dcom.Parameters.AddWithValue("@DateSince", TextBox12.Text)
Dcom.Parameters.AddWithValue("@DateTerm", TextBox13.Text)
Dcom.Parameters.AddWithValue("@TruckID", TextBox14.Text)
Dcom.Parameters.AddWithValue("@Commants", TextBox15.Text)
Dcom.Parameters.AddWithValue("@Image", DriverImage)
Dcom.Parameters.AddWithValue("@ID", Label3.Text)
Dcom.ExecuteNonQuery()
Dcon.Close()

I spent hours on google and i am unable to resolve this issue this are my Field name ID, First, Last, Company, Addr, City, ST, Zip, MobileP, HomeP, Email, DL, DateSince, DateTerm, TruckID, Commants, Image they all TEXT

Can anyone could tell me what is wrong with this syntax

The words FIRST and IMAGE are reserved keywords in MS-Access Jet SQL. If you want to use them you should use square brackets around them.

Also the syntax for the where clause is wrong. (But this is probably just a typo)

Dcom = New OleDbCommand("UPDATE Drivers SET " + 
     "ID=?,[First]=?,Last=?,Company=?,Addr=?,City=?," + 
     "ST=?,Zip=?,MobileP=?,HomeP=?,Email=?,DL=?,DateSince=?," + 
     "DateTerm=?,TruckID=?,Commants=?,[Image]=? WHERE ID=?", Dcon)

There is a syntax problem at the end of your sql query:

... WHERE ID = ID=?

I assume that should be

WHERE ID = ?

You're adding the ID parameter to your parameter list twice, once at the beginning and end.

Dcom.Parameters.AddWithValue("@ID", Label3.Text)
...
Dcom.Parameters.AddWithValue("@ID", Label3.Text)

Get rid of one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM