MySql.Data.MySqlClient.MySqlException: Parameter ‘@id’ must be defined
May 10
.NET, ASP.NET, MySQL 24 Comments
I found out a funny thing today. Or maybe not so funny when I think about it…
The order page on one of my sites have not been working since early November, almost 6 months!! As I’m sure you understand it’s not a very busy site, but usually I get a few orders per month so I should have noticed this a long time ago…
The site is running on IIS/ASP.NET and MySQL server 5.0.45 using the MySQL .NET Connector and I’m using a SQL statement like this on the page:
START TRANSACTION;
INSERT INTO table1 (companyname,street,city,zip) VALUES (‘XS Tech’,'test’,'test’,’12345′);
SELECT @id:=id FROM table1 where id = last_insert_id();
INSERT INTO table2 (id,contactname) VALUES (@id,’Max”);
COMMIT;
This statement was throwing an exception error: “MySql.Data.MySqlClient.MySqlException: Parameter ‘@id’ must be defined”
Since I could not really remember – ie had not properly documented – what changes I may have done to that page six months ago, it made trouble shooting a bit harder. But to make a long story short(er) – It turns out I had upgraded the MySQL .NET Connector and this was causing the error. The site was using MySql.Data.dll version 5.2.1.0. I tried to upgrade to 6.0.3.0 to no avail. But when downgrading to 5.1.4.0 it started to work just fine again.
Finally, I found that this was indeed a change in the .Net Connector. (I’m not sure what version number but starting from 5.2.0 would be my guess)
Starting from version 5.2.2 of the Connector you should add the Allow User Variables=True Connection String Setting in order to use User Defined Variables in your SQL statements.
Example of Connection String:
Database=testdb;Data Source=localhost;User Id=root;Password=hello;Allow User Variables=True

Jul 22, 2009 @ 12:01:45
Thanks – just found this through Google and it saved me a hell of a lot of time (as I just upgraded .NET connector versions and my stuff broke for a similar reason).
Sep 22, 2009 @ 23:05:23
Thank you. This is exactly what I needed.
Oct 27, 2009 @ 17:21:15
This was very helpful, I was having exactly the same problem. Thanks.
Dec 03, 2009 @ 15:14:29
Many thanks, i was getting this error after upgrading the Mysql Connector… still i think this is foolish, Mysql user variable have always been defined by using an ‘@’, they should have used some other method to identify user parameters…
Dec 16, 2009 @ 09:23:27
thanks..but still the conector MUST BE FXED …! THIS IS A BUG
Jun 08, 2010 @ 11:03:52
Thanks for this. You made my day
Jul 21, 2010 @ 11:13:20
Thanks for this
Oct 29, 2010 @ 14:19:11
Great help, thanks.
The same solution works when you use SET clause expressions with the BulkLoader.
Nov 02, 2010 @ 18:33:10
Thank you so much for the hint! Spent few hours on that earlier and couldn’t figure out. THANKS!
Dec 10, 2010 @ 20:17:03
AHAHAHAHHA Thank you
I was using a local var to store last_insert_id within a transaction and was battling thinking the last_insert_id / my sql syntax must be the problem. Low and behold, upgrading is a bitch
Dec 17, 2010 @ 14:11:28
This article saved my bacon, I’ve got the driver running database create scripts and this kept happening – got to love how to ‘do’ anything in mysql you have to stuff something in the connection string.
Jan 13, 2011 @ 13:17:28
Thank you very much, God bless you!
Mar 23, 2011 @ 16:27:46
Thanks a lot, we got into the same issues and you were spot.
Just a suggestion if you not mind, you need to have declared variables in your queries and could have done this way.
START TRANSACTION;
INSERT INTO table1 (companyname,street,city,zip) VALUES (‘XS Tech’,’test’,’test’,’12345′);
INSERT INTO table2 (id,contactname) VALUES (last_insert_id(),’Max”);
COMMIT;
Apr 11, 2011 @ 09:00:56
Thanks, this is exactly what I needed.
Apr 13, 2011 @ 16:37:00
Thanks a lot, you saved me time debugging this.
Jun 22, 2011 @ 15:18:35
thanks a lot..
Jul 08, 2011 @ 07:06:13
Your post saved me to rebuilt all my querys!!! THANKS!!!!
Jul 17, 2011 @ 16:26:56
Thank you! Thank you! Thank you!
Aug 03, 2011 @ 03:46:51
You saved my day.
Thanks alot.
Sep 06, 2011 @ 10:07:01
Awesome! Very help full. Thanks for posting.
Sep 21, 2011 @ 14:55:26
THANK YOU VERY VERY VERY MUCH!!!! YOU SAVED ME!!!
Sep 22, 2011 @ 02:34:02
thanks.. very thanks..
your post really help me
and i can smile again heu heu heu
Nov 06, 2011 @ 01:26:32
Saved me a ton of time! Thanks for posting this. Glad you came up near the list top with my Google Search phrase, which was very close to what you posted. -mj
Dec 14, 2011 @ 14:08:33
Holy Cow – thanks for this, close to pulling all hair after hours of wondering…this solved it.