ORA-01722 with ODP and Command Parameters
March 3, 2006
This week I was working on a section of code using C#, the Oracle .NET Provider (ODP) and some good old parameters in a SQL statement. The code was pretty straight forward (this is from memory without the assistance of intellisense– so apologies for any syntax errors):
string sql = ”INSERT INTO MyTable (TextField1, NumberField2) :param1 :param2″;
OracleCommand cmd = new OracleCommand(sql, this.conn);
cmd.Parameters.Add(”param2″, OracleDbType.Int32);
cmd.Parameters.Add(”param1″, OracleDbType.nVarChar2);cmd.Parameters["param2"].value = 12;
cmd.Parameters["param1"].value = “My Text”;cmd.ExecuteNonQuery();
Everything seemed good to go, but when I executed the query, I got ORA-01722: Invalid number.
When you check out the appropriate page on http://www.ora-code.com it reads, “The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal.”
Well, my parameter value was obviously a number, so I thought I was picking the wrong field type for the parameter. I tried all sorts of things– Int64, Int16, Long, etc. I always got the same error. Then I learned a helpful little tidbit about the OracleCommand object in ODP:
Named parameters are turned off by default.
So even though I’m referring to my parameters by name and setting their values by name– when the SQL command was running, it was plugging the parameters in by the order in which you add them to the command object. So in the above example, my value for param2 was what was getting plugged in for param1 and vice versa. As a result, my SQL was trying to insert “My Text” into a number field. Yup, I’d say that’s an invalid number!
Luckily, ODP does support named parameters (don’t believe news group postings from 2003 that say otherwise)– you have to turn it on with a cmd.BindByName = true; call.
Entry Filed under: ORA-01722, Oracle, Oracle .NET Provider. .
7 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed






1.
Griffin | August 25, 2006 at 11:16 am
This was some great advice! I’ve just spent hours trying to figure out why my code wouldn’t work and all I was missing was the cmd.BindByName parameter.
I was even plugging the the parameters in the correct order, but it still wouldn’t work.
Anyway, just thought I’d show a little appreciation for the advice. So thanks alot
2.
tgaw | August 25, 2006 at 12:42 pm
Thanks! I’m glad the post helped!
3.
Reply | September 15, 2006 at 1:54 pm
THANK YOU SO MUCH! I also just spent hours trying to find out why the procedure would run but not get the parameters. No other examples even mentioned this command property, lame!
rob lingstuyl
4.
tgaw | September 15, 2006 at 4:18 pm
You’re welcome! Glad it helped!
5.
Donna | August 9, 2007 at 4:06 pm
This had me stumped as well! Awesome stuff!
6.
mutenroid | October 29, 2007 at 5:29 am
Thank you very much guy!!
Now works fine.
7. 3 Year Blogiversary &laqu&hellip | March 3, 2009 at 1:03 am
[...] Well I could just let it remain empty, now could I? So I did a post. It was riveting. Brace yourself– ORA-01722 with ODP and Command Parameters. [...]