Friday, February 8, 2013

.NET - CLI0100E Wrong number of parameters

So, here I am again....

I was working on a .NET project where I would get my data from the database. Everything worked fine until one one the query gave me the following Exception:

CLI0100E  Wrong number of parameters. SQLSTATE=07001

Now, that error message seemed descriptive enough where it's telling me that the number of parameters are passed. I assure you the sql and the number of parameters passed were correct.

I looked and looked on the web to see what else could cause this error, but nothing helped my particular case. I even engaged co-workers to see what would be causing this error. With their helped the conclusion was the wrong declaration of the parameter list.

For simplicity purposes the sql would look like this:

string sql = "Select field1, field2, from table1 where field3 = ?";

Code:
---------------------------------------------------------------------
List<Parameter> list = new List<Parameter>(); //<-- Bad boy
list.add(new Parameter(DBType.String, "stringvalue");

IDataReader dr = db.ExecuteReader(sql, list);
---------------------------------------------------------------------

ExecuteReader method signature is (string, IList<IParameter>) and in theory IList and IParameter should resolve fine when passing List<Parameter>. The compiler doesn't even care.
However, when executing it won't get recognized as a parameter causing the exception.
The fix was simple, but frustrating to find. The simple thing to do was to change the list declaration as follows:

List<IParameter> list = new List<IParameter>(); //<-- Good boy

Hopefully this will help someone.





No comments:

Post a Comment