insert sql command

insert into sql command error

Page 1 of 1

4 Replies - 5186 Views - Last Post: 04 May 2010 - 06:30 PM Rate Topic: -----

#1 darkness3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

insert sql command

Posted 04 May 2010 - 02:36 PM

Hello,

I am working on a project for my final in vb.net and I have a weird error when I try to insert info into my database. When I run the application it errors out on command.ExecuteNonQuery()and the error is Syntax error in INSERT INTO statement.

I have the fields in my database are set as text at 255 chars. I have both inmports statments at the top, and as far as I can tell my statement is right.

Here is my code.

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =../../Base/LogTextCopy.accdb;Persist Security Info=False;")
            Dim StrSQL As String = "INSERT INTO TIMEANDDATE(DATE, TIME) values(@date, @time)"
            Dim command As New OleDbCommand()
            command.CommandText = StrSQL
            command.Connection = con
            command.Parameters.Clear()

            Dim ParameterDate As New OleDbParameter
            ParameterDate.ParameterName = "@date"
            ParameterDate.OleDbType = OleDbType.Char
            ParameterDate.Size = 100
            ParameterDate.Direction = ParameterDirection.Input
            ParameterDate.Value = strDate

            Dim ParameterTime As New OleDbParameter
            ParameterTime.ParameterName = "@time"
            ParameterTime.OleDbType = OleDbType.Char
            ParameterTime.Size = 100
            ParameterTime.Direction = ParameterDirection.Input
            ParameterTime.Value = strTime

            command.Parameters.Add(ParameterDate)
            command.Parameters.Add(ParameterTime)

            con.Open()
            command.ExecuteNonQuery()
            con.Close()

            DataBaseUpdate()



Is This A Good Question/Topic? 0
  • +

Replies To: insert sql command

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9496
  • View blog
  • Posts: 35,844
  • Joined: 12-June 08

Re: insert sql command

Posted 04 May 2010 - 03:23 PM

Okay.. good information so far - how about the actual error message? :)

This post has been edited by modi123_1: 04 May 2010 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 darkness3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: insert sql command

Posted 04 May 2010 - 03:34 PM

View Postmodi123_1, on 04 May 2010 - 02:23 PM, said:

Okay.. good information so far - how about the actual error message? :)


System.Data.OleDb.OleDbException was unhandled
  ErrorCode=-2147217900
  Message=Syntax error in INSERT INTO statement.
  Source=Microsoft Office Access Database Engine
  StackTrace:
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(Commandbehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(Commandbehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


This post has been edited by darkness3: 04 May 2010 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: insert sql command

Posted 04 May 2010 - 04:35 PM

I would say that Date and Time are reserved keywords, try surrounding them with [] in the insert statement.
"INSERT INTO TIMEANDDATE([DATE], [TIME]) values(@date, @time)"


In my opinion, you should actually change the field names in the database to something like LogDate, LogTime so as not to conflict with keywords but the above fix should work.

This post has been edited by CharlieMay: 04 May 2010 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 darkness3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: insert sql command

Posted 04 May 2010 - 06:30 PM

yeah that worked, should have known that lol. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1