Why is Access Database Engine giving a weird error?

245 views Asked by At

The following code crashes with this error:

System.Data.OleDb.OleDbException: 'Too few parameters. Expected 1'

Dim selectString As String = "SELECT * FROM Products WHERE id = ?;"
Dim cmd As OleDbCommand = New OleDbCommand(selectString, dbOleDB)
cmd.Parameters.AddWithValue("ID", id)
Dim reader As OleDbDataReader = cmd.ExecuteReader()

If I do a repair on the Access Database Engine installation the error disappear for a day.
The same thing happens on multiple machines running different versions of Windows.
This problem started about 2 weeks ago.

Anyone have any idea whats happening?

1

There are 1 answers

13
Jimmy Smith On

OleDbCommand does not behave right with named parameters.

Try this instead,

Dim selectString As String = "SELECT * FROM Products WHERE id = ?;"
Dim cmd As OleDbCommand = New OleDbCommand(selectString, dbOleDB)
cmd.Parameters.Add("@ID", OleDbType.BigInt).Value = id; '@ID essentially means nothing here.  The Adds you make have to be sequential
Dim reader As OleDbDataReader = cmd.ExecuteReader()