Click here to Skip to main content
15,562,561 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have "(Syntax error in UPDATE statement.)" when I updating the record in access database with VB.net code.Thanks for helping.

What I have tried:

VB
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
     myconnection.ConnectionString =
     "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Horng Woei\Documents\Klinik OCEANA.mdb"

     myconnection.Open()
     Dim cmd As New OleDbCommand
     Dim ds As New DataSet
     da = New OleDbDataAdapter("select * from [PatientData]", myconnection)
     If da.Fill(ds) Then

         Dim query As String = "update [PatientData] set [PatientName],[Age],[Gender],[Nationality],[ContactNo],[DOR],[IC],[Blood Type],[Weight(kg)],[Height(cm)],[Allergies]
         = '" + txtName.Text + "','" + txtAge.Text + "','" + cboGender.Text + "','" + cboNationality.Text + "','" + txtContact.Text + "','" + txtDOR.Text + "','" + txtIC.Text + "','" + cboBloodType.Text + "','" + txtWeight.Text + "','" + txtHeight.Text + "','" + txtAllergies.Text + "'
         where [PatientID] = '" + txtID.Text + "'"

         cmd.Parameters.AddWithValue("@PatientName", txtName.Text)
         cmd.Parameters.AddWithValue("@Age", txtAge.Text)
         cmd.Parameters.AddWithValue("@Gender", cboGender.Text)
         cmd.Parameters.AddWithValue("@Nationality", cboNationality.Text)
         cmd.Parameters.AddWithValue("@ContactNo", txtContact.Text)
         cmd.Parameters.AddWithValue("@DOR", txtDOR.Text)
         cmd.Parameters.AddWithValue("@IC", txtIC.Text)
         cmd.Parameters.AddWithValue("@Blood Type", cboBloodType.Text)
         cmd.Parameters.AddWithValue("@Weight(kg)", txtWeight.Text)
         cmd.Parameters.AddWithValue("@Height(cm)", txtHeight.Text)
         cmd.Parameters.AddWithValue("@Allergies", txtAllergies.Text)
         cmd = New OleDbCommand(query, myconnection)

         cmd.ExecuteNonQuery()
         cmd.Dispose()
         MsgBox("Data Updated Successfully!")
         myconnection.Close()
         filterrecords("")
     End If

 End Sub
Posted
Updated 3-Apr-20 22:00pm
v3

2 things:
1) Never 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. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
That code is completely wrong: you are concatenating, and setting up parameters you don't use!

2) That isn't the syntax of an UPDATE, or even close. An UPDATE statement looks like this:
SQL
UPDATE MyTable SET MyColumn1 = @ParameterValueForColumn1, MyColumn2 = @ParameterValueForColumn2 WHERE ...
 
Share this answer
 
Because the problem can be from any of user input and depend on table structure, it is impossible to guess what is wrong.
VB
Dim query As String = "update [PatientData] set [PatientName],[Age],[Gender],[Nationality],[ContactNo],[DOR],[IC],[Blood Type],[Weight(kg)],[Height(cm)],[Allergies]
         = '" + txtName.Text + "','" + txtAge.Text + "','" + cboGender.Text + "','" + cboNationality.Text + "','" + txtContact.Text + "','" + txtDOR.Text + "','" + txtIC.Text + "','" + cboBloodType.Text + "','" + txtWeight.Text + "','" + txtHeight.Text + "','" + txtAllergies.Text + "'
         where [PatientID] = '" + txtID.Text + "'"


Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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