2

I'm using OleDb to populate a DataTable. I'm trying to use a parameterized query, but it doesn't seem to work with a OleDbDataAdapter. Anyone have any suggestions?

cmd.CommandText = "SELECT A,B,C,D FROM someTable WHERE A=@A AND D BETWEEN @D1 AND @D2";
cmd.Parameters.Add("@A", OleDbType.VarChar).Value = "1234567";
cmd.Parameters.Add("@D1", OleDbType.DBDate).Value = "02/01/2011";
cmd.Parameters.Add("@D2", OleDbType.DBDate).Value = "01/31/2012";

A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
System.Data.OleDb.OleDbException (0x80040E11): [DB2] SQL0206N  "@A" is not valid in the context where it is used.  SQLSTATE=42703
1
  • Are you sure your "A" column is a text field, not numeric? Your D1 and D2 parameter values should probably be dates, not strings. Commented Nov 14, 2011 at 15:20

3 Answers 3

5

See http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx:

"The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters."

So you cannot use the @Parameter syntax, you have to indicate parameters with question marks, and assign your parameter values in the exact same sequence as they appear in the query.

Sign up to request clarification or add additional context in comments.

4 Comments

That's true, but the parameters are in order. It should still work.
The documentation says you should use question marks, so according to that, it shouldn't.
I've done it a million times. Named parameters will work on the surface, but behind the scenes, they are question marks, so the "order" is what is important. Microsoft goofed that one up pretty good.
I am familiar with the Odbc and Oracle providers, they don't accept @parameter. I assumed by reading the documentation that this would be the same for OleDb, and even if it somehow works, it is never a good idea to use something that does not comply to the documentation. The next version may cause your application to crash, leaving you with a nasty bug to track.
0

For those looking for help when their query doesn't even have a parameter, check if your column names are valid.

see @TheTerribleProgrammers answer at OleDbException: no value given for parameters when no parameters are defined

Comments

-1
public static DataTable getDataGridList(string strCmd)
    {

        openConnection(conn);
        OleDbDataAdapter DADet = new OleDbDataAdapter(strCmd, conn);
        DataTable DTDet = new DataTable();
        DADet.Fill(DTDet);
        closeConnection(conn, null);
        return DTDet;
    }

1 Comment

Welcome to SO. Please try to explain your answer in words, not just in code. What does you suggestion do and how does it differ from the code in the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.