sabari
sabari

Reputation: 2625

Syntax error in insert into statement - in VB.NET

I am trying to insert a record into MS Access db, with the below code. I have used the same type of code several times in my project. But I dont know why it is erroring , saying that there is a syntax error. Some one please suggest me where the code is wrong.

Try
                If MainForm.con.State = ConnectionState.Closed Then
                    MainForm.con.Open()
                End If
                Dim cmdText As String
                cmdText = "insert into tblBottling(bottlingDate,workerName,seed,size,noOfBottles,timeTaken,remarks) values(?,?,?,?,?,?,?)"
                Dim command As OleDbCommand = New OleDbCommand(cmdText, MainForm.con)

                command.Parameters.AddWithValue("@bottlingDate", botDate.Value.ToString("dd-MM-yy"))
                command.Parameters.AddWithValue("@workerName", workerCB.SelectedItem.ToString)
                command.Parameters.AddWithValue("@seed", seedCB.SelectedItem.ToString)
                command.Parameters.AddWithValue("@size", botSizeCB.SelectedItem.ToString)
                command.Parameters.AddWithValue("@noOfBottles", CInt(noOfBot.Text))
                command.Parameters.AddWithValue("@timeTaken", timeTakenTxt.Text)
                command.Parameters.AddWithValue("@remarks", remarksTxt.Text)

                command.ExecuteNonQuery()
                MainForm.con.Close()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

Upvotes: 0

Views: 1588

Answers (3)

Sultan Alkheel
Sultan Alkheel

Reputation: 1

try to change the columns names when you create tables avoiding keywords like username like i did 😅

Upvotes: 0

Dawud Abdul Manan
Dawud Abdul Manan

Reputation: 234

`cmdText = "insert into tblBottling([bottlingDate],[workerName],[seed],[size],[noOfBottles],[timeTaken],[remarks]) values(?,?,?,?,?,?,?)"

Please always use square brackets for your columns incase of such error.

Because you cannot memorise all the keywords.

Good Luck

Upvotes: 0

Steve
Steve

Reputation: 216323

Size is a reserved keyword for MS-Access. If you want to use that word as column name then you should always enclose it between square brackets

cmdText = "insert into tblBottling
          (bottlingDate,workerName,seed,[size],noOfBottles,timeTaken,remarks) 
          values(?,?,?,?,?,?,?)"

Upvotes: 2

Related Questions