OLEDB connection throws exception "Too few parameters"

Viewed 237

I have the following INSERT query being executed against an Access DB...

using (var cmd = conn.CreateCommand())
{
    try
    {
        cmd.CommandText = @"INSERT INTO mtConcepts (
                                conceptid,
                                [text],
                                incomplete)
                            VALUES (
                                @conceptid,
                                @text,
                                false)";
        cmd.Parameters.Add("@conceptid", OleDbType.BigInt).Value = concept.Id.Value;
        cmd.Parameters.Add("@text", OleDbType.LongVarWChar).Value = cG.ToString(SaveOptions.DisableFormatting);
        await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
    }
    catch (AggregateException exception)
    {
        ...
    }
    catch (OleDbException exception)
    {
        ...
    }
}

An OleDbException is caught with the message...

Too few parameters. Expected 2.

...and I can't understand why.

This is code which was written about 7 years ago and has worked until... sometime recently.

Furthermore, if I don't parameterise the query and instead pass the values directly in the SQL then it works.

Update

I got this working by installing an older version of the Microsoft Access Database Engine 2010 Redistributable. But still, I'm puzzled - before installing this 2010 version I was using the connection which comes with the latest version of Access (installed via Office365) so it should still have worked.

1 Answers

Just as help. I had the same issue, So I changed the parameters name to question Mark

it works fine; for example:

OdbcCommand cmd = new OdbcCommand("select * from Users where user_id=? and passwd=?", conn); // *

cmd.Parameters.Add(new OdbcParameter("@UserID", userid));

cmd.Parameters.Add(new OdbcParameter("@Password", password));

OdbcDataReader reader = cmd.ExecuteReader();
Related