Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

I am getting syntax error for below statement. But I can't find it, Please check and let me know if you can find it. Statement is;

Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)", sqlconn)

What I have tried:

My Entire Code is;

VB
private Sub cmdStockAdd_Click(sender As Object, e As EventArgs) Handles cmdStockAdd.Click
        Dim res As String

        res = MsgBox("Do you want to Add this record", vbYesNo, "SFA-Inventory Add")
        If res = vbYes Then
            Try
                Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Data\SFA\My Project\SFA Inventory\SFA Inventory\SFA Inventory.accdb")
                    Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)", sqlconn)
                        sqlquery.Parameters.AddWithValue("@IMEI No", TextBox2.Text)
                        sqlquery.Parameters.AddWithValue("@Product Name", TextBox1.Text)
                        sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
                        sqlquery.Parameters.AddWithValue("@Invoice No", TextBox3.Text)
                        sqlquery.Parameters.AddWithValue("@Invoice Date", DateTimePicker1.Value.ToShortDateString())
                        sqlquery.Parameters.AddWithValue("@Mobile No", TextBox4.Text)
                        sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)
                        sqlconn.Open()
                        sqlquery.ExecuteNonQuery()
                        MsgBox("Records inserted successfully", vbOKOnly, "Records inserted")
                        TextBox2.Text = ""
                        TextBox1.Text = ""
                        ComboBox1.Text = ""
                        TextBox3.Text = ""
                        DateTimePicker1.Value = DateTime.Now
                        TextBox4.Text = ""
                        TextBox5.Text = ""
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            Show()

        End If
    End Sub
Posted
Updated 11-Dec-18 2:26am
v2
Comments
Afzaal Ahmad Zeeshan 11-Dec-18 6:31am    
Try executing this query inside the Access database itself, and see if it works fine there.
Bryian Tan 11-Dec-18 7:56am    
The syntax should be "INSERT INTO [SFA Inventory] ..."

It is a bad idea to put spaces in table and column names. If you do that, you have to enclose the names in square brackets to make sure the name is considered with the spaces:
INSERT INTO [SFA Inventory] (IMEINo, ProductName, Supplier, InvoiceNo, InvoiceDate, MobileNo, AppVersion) VALUES (@IMEINo, @ProductName, @Supplier, @InvoiceNo, @InvoiceDate, @MobileNo, @AppVersion)


Also, remove all of the spaces in your named parameters:
sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@AppVersion", TextBox5.Text)


You're also make another grievous mistake. NEVER directly use the values of textboxes in your queries. ALWAYS validate the data that's entered and use the validated data in the parameters.
 
Share this answer
 
try
VB
Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(@IMEINo,@ProductName,@Supplier,@InvoiceNo,@InvoiceDate,@MobileNo,@AppVersion)", sqlconn)
sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@AppVersion", TextBox5.Text)
 
Share this answer
 
Comments
Member 14084723 11-Dec-18 1:45am    
No. It didn't work. same error message occurred. I am going to connect Access DB.
Karthik_Mahalingam 11-Dec-18 1:49am    
what is the error message?
Member 14084723 11-Dec-18 2:21am    
"Syntax error in INSERT INTO Statement"
Karthik_Mahalingam 11-Dec-18 4:31am    
what is the table name
Hello ,

Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)", 


with out space

and here with
sqlquery.Parameters.AddWithValue("@IMEI No", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@Product Name", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@Invoice No", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@Invoice Date", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@Mobile No", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)



Change it to

sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)
 
Share this answer
 
Comments
Member 14084723 11-Dec-18 2:47am    
did. But still I get the error message.
Member 14084723 11-Dec-18 5:12am    
Find the Solution. Thanks for your support.

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