Results 1 to 7 of 7

Thread: OledbDataAdapter insert error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    OledbDataAdapter insert error

    I'm calling dataadapter.update() but it says "System.Data.OleDb.OleDbException: 'Syntax error in INSERT INTO statement.'" but the insert statement was generated with an OleDbCommandBuilder so I can't figure out where the error actually is. This is where the dataadapter is built
    Code:
       Public Function Gettransaction_lines(Optional id As Long = -200) As DataTable
            Dim query As String
            Dim dt As New DataTable
            query = "SELECT * FROM transaction_lines WHERE id=@id"
            Dim conndb = New OleDbConnection(connStr)
            Dim cmd = New OleDbCommand(query, conndb)
            With cmd.Parameters
                .Add("@id", OleDbType.BigInt).Value = id
            End With
            transaction_linesAdapter = New OleDbDataAdapter(query, conndb)
            transaction_linesAdapter.SelectCommand = cmd
            Dim cmdB = New OleDbCommandBuilder(transaction_linesAdapter)
            transaction_linesAdapter.Fill(dt)
    
            Return dt
        End Function
    this is where I get the table
    Code:
            dtNewtransaction_lines = Gettransaction_lines(-200)
    -200 so I end up with the table structure but no actual rows

    Then I create newrows with
    Code:
            transaction_linesNewRow = dtNewtransaction_lines.NewRow
    Fill the row with data from use selections (no manual entry so no SQL injection possible) then add back to the table
    Code:
            dtNewtransaction_lines.Rows.Add(transaction_linesNewRow)
    Then call the update that generates the error
    Code:
    transaction_linesAdapter.Update(dtNewtransaction_lines)
    I don't see anything that could be wrong so it is probably a misleading error message and I am clueless. Any idea what I should try?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: OledbDataAdapter insert error

    The issue will be that one or more of your column names are reserved words or else contain spaces or other special characters. Those column names need to be escaped in the SQL code, which won't happen automatically. You have two options. You can write out your column list in your query and escape those columns that need it, then the command builder will follow your lead. Alternatively, you can set the QuotePrefix and QuoteSuffix properties of the command builder and it will escape all the column names. For Access, the values would be an opening bracket and closing bracket respectively.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: OledbDataAdapter insert error

    By the way, it's generally best to avoid such column names in the first place. Then there can't be an issue.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Re: OledbDataAdapter insert error

    AH that was it! It didn't like the column name "number". I thought it was a syntax error in the INSERT command like "INSERTINTO" or something like that. I remember running into a similar reserved word problem like that before now. From now on if I use a real word on a column I'll add something like in this case I changed "number" to "numitems".

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: OledbDataAdapter insert error

    I wouldn't use "numitems" as a column name. "Count" is generally a good name for a column or the like that contains the count of items. If appropriate, you can qualify that with what's being counted, e.g. "ItemCount".

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Re: OledbDataAdapter insert error

    In this case it isn't counting. Its the number of items supplied by the user.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: OledbDataAdapter insert error

    Quote Originally Posted by pmeloy View Post
    In this case it isn't counting. Its the number of items supplied by the user.
    That's a count. Why do you think that collections have a Count property? It's the number of items in the collection. If the user supplied some items to you, how would you determine how many there were? You'd count them, right? The result of that count is the value you're storing in your database column.

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