Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
am trying to insert into a table in my database and i get this error of incorrect syntax near ','. below is my code

What I have tried:

<pre>con = New SqlConnection(cs)
            con.Open()
            Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (" & txtBillID.Text & ",'" & txtBillNo.Text & "'," & txtCheckInID.Text & ",@d2,@d1)"
            cmd = New SqlCommand(cb1)
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@d1", txtNotes.Text)
            cmd.Parameters.AddWithValue("@d2", dtpBillDate.Value)
            cmd.ExecuteReader()
            con.Close()
Posted
Updated 2-Sep-18 7:27am

It is impossible to know what is exactly your query because it depend on the values used to build the query.
Only the debugger can show what is the real query, then we can say what is the problem.
VB
Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (" & txtBillID.Text & ",'" & txtBillNo.Text & "'," & txtCheckInID.Text & ",@d2,@d1)"

You are happily mixing concatenation and parameters in same query, that is bad.

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[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

Secondary problem, it is impossible to know what is exactly your query because it depend on the values of the parameters.
Only the debugger can show what is the real query.
 
Share this answer
 
Why not keep it clean instead of mixing the Parameterized and regular query with string concatenation? stick with Parameterized. This will save you some headache down the road like SQL injection vulnerability and avoid the syntax issue.

Another observation, look like the txtBillID,txtBillNo,txtCheckInID should be integers, you might need to update the code to convert the string into integer before inserting it into the table.

VB
Dim cb1 As String = "insert into CheckOut_Room(ID,BillNo,CheckInID,BillDate,Notes ) VALUES (@d1, @d2, @d3, @d4,@d5)"
        cmd = New SqlCommand(cb1)
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@d1", txtBillID.Text)
        cmd.Parameters.AddWithValue("@d2", txtBillNo.Text)
        cmd.Parameters.AddWithValue("@d3", txtCheckInID.Text)
        cmd.Parameters.AddWithValue("@d4", dtpBillDate.Value)
        cmd.Parameters.AddWithValue("@d5", txtNotes.Text)
 
Share this answer
 
v3

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