C Sharper
C Sharper

Reputation: 8626

inserting datetime in oledb through vb.net , query gives error

My DBStructure is as follows:

enter image description here

I want to insert a record in it, so i am firing following query:

insert into LocalBarcode (PescaLocation,Barcode,TimeStamp,IsUpload)values('1','11','10/17/2013 

6:08:57 PM') 

But its giving me syntax error: as

Syntax Error in Insert into.

For IsUpload, i have kept default value to 'N', hence i am not sending it from here.

LBID is autoincreament.

Please help.

VB.NET query:

Dim sqlInsertBarcode = "insert into LocalBarcode 

(PescaLocation,Barcode,TimeStamp,IsUpload)values('" & pescaLocation & "','" & 

txtBarcode.Text.Trim.Replace("'", "''") & "','" & Now() & "') "

Upvotes: 0

Views: 850

Answers (1)

JLott
JLott

Reputation: 1828

You could also try something more like this:

Try
        dbConnection.Open()
        Dim cmd As New OleDbCommand
        cmd.CommandText = "INSERT INTO LocalBarcode ( PescaLocation, Barcode, TimeStamp, " & _
                          "IsUpload) VALUES " & _
                          "(@pescalocation, @barcode, @whatever, @whatever)"
       cmd.Parameters.AddWithValue("@Pescalocation", pescalocation)
       cmd.Parameters.AddWithValue("@Barcode", txtBarcode.text)
       cmd.Parameters.AddWithValue("@TimeStamp", whatever)
       cmd.Parameters.AddWithValue("@IsUpload", whatever)
       cmd.ExecuteNonQuery()
   Catch ex As Exception
   Finally
       dbConnection.Close()
   End Try

I would go ahead and make variables for your "Now" and "IsUpload" so that you can use AddWithValue easier.

Upvotes: 2

Related Questions