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
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).
Thank you. This is exactly what I needed.
This was very helpful, I was having exactly the same problem. Thanks.
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…
thanks..but still the conector MUST BE FXED …! THIS IS A BUG
Thanks for this. You made my day 🙂
Thanks for this
Great help, thanks.
The same solution works when you use SET clause expressions with the BulkLoader.
Thank you so much for the hint! Spent few hours on that earlier and couldn’t figure out. THANKS!
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
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.
Thank you very much, God bless you!
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;
Thanks, this is exactly what I needed.
Thanks a lot, you saved me time debugging this.
thanks a lot..
Your post saved me to rebuilt all my querys!!! THANKS!!!!
Thank you! Thank you! Thank you!
You saved my day.
Thanks alot.
Awesome! Very help full. Thanks for posting.
THANK YOU VERY VERY VERY MUCH!!!! YOU SAVED ME!!!
thanks.. very thanks..
your post really help me 🙂
and i can smile again heu heu heu
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
Holy Cow – thanks for this, close to pulling all hair after hours of wondering…this solved it.
God bless you! =)
This made my work a bit easier today.
Thanks
Great solution, i was browsed by hours and, thanks a lot man, when I added that line of code I was all great.
thank you very much..^^
finally, i could solve my problem..
Thank you so much!
Hi! You do not have idea how much helped me. I wanna really thank you.
heyy its very useful 🙂 thank u so much
Thank you very much, God bless you! (2)
Thanks very much!
this problem continue on mysql-connector-net-6.6.4
Thank you so much!
Thanks a Lot!
Thank you for posting this. Very helpful.
Thank you. Been struggling with this
I’m using ASP.NET4 with MySQL via Visual Studio 2010 and everything worked fine locally. As soon as I uploaded to my host, I had the same parameter errors.
Adding “Allow User Variables=True” to my connection string fixed everything.
THANK YOU!!!
I’m still getting getting below error message, even with this code added. I’m using ASP.NET C# Express version.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
THANKSSSSS!
wow!!!…. Very helpfull
Thank U,
i would have never been able to figure it out by myself
Thanks a lot…
Spent lots off time to fix this…
Finally got ..Thansk
@Tecniservicios L & S
The same for me!!! Why Google didn’t present this post in the first position?!?!?!?
🙂
I was getting crazy!!! This solution saved me….
6 years after original post … And still saving the day.
Gracias!!, I was getting so frustrated 😀
Thank u, my problem solved
thank you very much
Thanks a lot!!!!
Found your post via StackOverflow (http://stackoverflow.com/questions/5524632/how-can-i-use-a-mysql-user-defined-variable-in-a-net-mysqlcommand), and it saved my bacon when using a row number variable in my query. Thanks for the tip!
This bug has bothered me for the last 5 hours. Thank you so much.
For info:
My seeding of an automatic initialized MySQL database, using EF, was failing on my AppVeyor build server but not in local development. Simply caused by the missing tag in the connection string in one of the configurations.
Thanks for this article! You’ve saved me a lot of time 🙂
Grazie mille.
Sergio
Thanks man it saved a lot of time. Nice Article.
Thanks ~ you save me alot of time
Thanks! It’s working.
Thanks dude, you have saved my life.
Thanks.. saved my life…
Chido Viejo
Thanks, I spent 8 hours trying to understand that the problem was not in the stored procedure parameters, but the connector manager!
thansk
Amazing that it was something so simple! I was trying to call a Stored Proc and adding Allow User Variables=True to the connection string stopped the error I kept getting. Thank you!! I can’t believe that over 10 years later that this is STILL an issue?!
Thanks brother, this is just what I needed, you saved me, keep it up! Good for you!
2020 anyone?