Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to coding, got a "Syntax error in INSERT INTO statement" after i clicked the button.Please give me some advice and thanks for helping !

What I have tried:

VB
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        myconnection.ConnectionString =
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Horng Woei\Documents\Klinik OCEANA.mdb"
        Dim Reader As OleDbDataReader
        If (txtName.Text = "" AndAlso txtAge.Text = "" AndAlso cboGender.Text = "" AndAlso
            cboNationality.Text = "" AndAlso txtContact.Text = "" AndAlso txtDOR.Text = "" AndAlso txtIC.Text = "" AndAlso
            cboBloodType.Text = "" AndAlso txtWeight.Text = "" AndAlso txtHeight.Text = "" AndAlso txtAllergies.Text = "") And (cboBloodType.SelectedIndex = 0 AndAlso
            cboGender.SelectedIndex = 0 AndAlso cboStatus.SelectedIndex = 0 AndAlso cboNationality.SelectedIndex = 0) Then
            MsgBox("Please fill in the form completely.")


        Else
            Try
                myconnection.Open()
                Dim query As String
                query = "Insert into [PatientData] ([PatientName],[Age],[Gender],[Marital Status][Nationality],[ContactNo],[DOR],[IC Number],[Blood Type],[Weight(kg)],[Height(cm)],[Allergies])
                Values ('" & txtName.Text & "','" & txtAge.Text & "','" & cboGender.Text & "','" & cboStatus.Text & "','" & cboNationality.Text & "','" & txtContact.Text & "','" & txtDOR.Text & "','" & txtIC.Text & "','" & cboBloodType.Text & "','" & txtWeight.Text & "','" & txtHeight.Text & "','" & txtAllergies.Text & "')"
                cmd = New OleDbCommand(query, myconnection)
                Reader = cmd.ExecuteReader

                MessageBox.Show("You have stored data successfully!")
                myconnection.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                myconnection.Dispose()
                txtName.Clear()
                txtAge.Clear()
                cboGender.ResetText()
                cboStatus.ResetText()
                cboNationality.ResetText()
                txtContact.Clear()
                txtDOR.Clear()
                txtIC.Clear()
                cboBloodType.ResetText()
                txtWeight.Clear()
                txtHeight.Clear()
                txtAllergies.Clear()
                refreshdata("")



            End Try
        End If

    End Sub
Posted
Updated 4-Apr-20 5:26am
v2

Don't do it like that! 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?

And by the time you have fixed that throughout your app, the chances are the problem you have noticed will have gone away as well ... because you will have noticed the missing comma.
 
Share this answer
 
v2
Quote:
Syntax error in INSERT into statement

Because of the way you build the SQL command, it is impossible to help you because user input is part of the command and we have no way to know what is the real command.

Temporary solution: The debugger will allow you to run the code step by step and to display/print the real query. You will understand the problem for the very user input.

Definitive solution: Use the syntax with parameters to prevent the command being sensible to user input.
-----
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