Results 1 to 6 of 6

Thread: [2005] Syntax error in INSERT INTO..in ado.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    8

    Resolved [2005] Syntax error in INSERT INTO..in ado.net

    hi,
    im trying out a simple code in which i add data to my database.....
    Code:
      Dim dr As DataRow
            strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\rs232\rem.mdb;User Id=;Password=;"
    
            Dim cmd As New OleDbCommand("INSERT INTO reminderdata (datetime, reminder)VALUES('" & txtdate.Text & "','" & txtreminder.Text & "')", New OleDbConnection(strconn))
    
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
    but its giving me error saying Syntax error in INSERT INTO statement

    so i tried the code given here

    Code:
    dr = ds.Tables(0).NewRow()  'Gets a reference to a new row.
            dr("datetime") = txtdate.Text
            dr("reminder") = txtreminder.Text
    
    
           ds.Tables(0).Rows.Add(dr)
           da.Update(ds)
           ds.AcceptChanges()
    and declared the insert command as

    Code:
    da.InsertCommand = New OleDbCommand("INSERT INTO reminderdata(datetime, reminder) VALUES(@datetime,@reminder)")
            da.InsertCommand.Connection = conn
            da.InsertCommand.Parameters.Add("@datetime", OleDbType.VarChar, 40, "datetime")
            da.InsertCommand.Parameters.Add("@reminder", OleDbType.VarChar, 40, "reminder")

    but i got the same error.....

    plz help me resolve the error.....
    Last edited by crystalsnehal; Apr 4th, 2008 at 07:01 AM.

  2. #2
    Lively Member
    Join Date
    Aug 2007
    Posts
    86

    Re: [2005] Syntax error in INSERT INTO..in ado.net

    try putting [] round your fieldnames..
    Code:
    Dim cmd As New OleDbCommand("INSERT INTO reminderdata ([datetime], [reminder])VALUES('" & txtdate.Text & "','" & txtreminder.Text & "')", New OleDbConnection(strconn))
    edit: it could be that datetime is a reserved word

  3. #3
    Hyperactive Member koolprasad2003's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    443

    Arrow Re: [2005] Syntax error in INSERT INTO..in ado.net

    Quote Originally Posted by crystalsnehal
    hi,
    Code:
      
            Dim cmd As New OleDbCommand("INSERT INTO reminderdata (datetime, reminder)VALUES('" & txtdate.Text & "','" & txtreminder.Text & "')", New OleDbConnection(strconn))
    is the datatype of datetime and reminder are DATETIME ???
    if no then do them first ...
    to insert datetime value don't give '" & txtdate.Text & "' double quote
    just give " & txtdate.Text & "

    double quote are used, when we insert varchar type data

    thankx
    koolprasad2003
    MCP, MCTS, Microsoft MVP [Asp.Net/IIS]

    For more .NET development tips visit .NET Tips

    If the post is useful then please Rate it

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    8

    Resolved Re: [2005] Syntax error in INSERT INTO..in ado.net

    Quote Originally Posted by Jrogers
    try putting [] round your fieldnames..
    Code:
    Dim cmd As New OleDbCommand("INSERT INTO reminderdata ([datetime], [reminder])VALUES('" & txtdate.Text & "','" & txtreminder.Text & "')", New OleDbConnection(strconn))
    edit: it could be that datetime is a reserved word

    Thanx for ur suggestion it worked.....

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    8

    Re: [2005] Syntax error in INSERT INTO..in ado.net

    Quote Originally Posted by koolprasad2003
    is the datatype of datetime and reminder are DATETIME ???
    if no then do them first ...
    to insert datetime value don't give '" & txtdate.Text & "' double quote
    just give " & txtdate.Text & "

    double quote are used, when we insert varchar type data

    thankx
    koolprasad2003
    no actually they both are of string type

  6. #6
    Hyperactive Member koolprasad2003's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    443

    Smile Re: [2005] Syntax error in INSERT INTO..in ado.net

    fine.....
    MCP, MCTS, Microsoft MVP [Asp.Net/IIS]

    For more .NET development tips visit .NET Tips

    If the post is useful then please Rate it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width