1
SELECT machine_id,operator_id,member_id,card_id,name,paid_amount,due_amount,paid_date,phone_number,@curRow := @curRow + 1 AS row_number
FROM transaction JOIN (SELECT @curRow := 0) r
where card_id=@card
order by Row_number desc limit 3 ;

my query is giving correct results when i run in Mysql workbench. But in program it's giving error

Parameter '@curRow' must be defined.

String query3 = "SELECT machine_id,operator_id,member_id,card_id,name,paid_amount,due_amount,paid_date,phone_number ,@curRow := @curRow + 1 AS row_number FROM transaction JOIN  (SELECT @curRow := 0) r where card_id=@card order by Row_number desc limit 3 ";

MySqlCommand command3 = new MySqlCommand(query3, con);
command3.Parameters.AddWithValue("@card", cardid);
using (MySqlDataReader rdr3 = command3.ExecuteReader())
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • 5
    post the relevant C# code please – Nathan Feb 03 '14 at 08:04
  • Each parameter will match a `MySqlParameter` object that must be assigned to `MySqlCommand` object. But since you didn't show your code, we can't know exactly what the problem is.. – Soner Gönül Feb 03 '14 at 08:08
  • This would *usually* be something like: `int cardId = ...` `cmd.Parameters.AddWithValue("card", cardId);` (repeat per parameter, also making sure that you use `DBNull.Value` and not `null` if you want to represent database-null); however! it varies a little between ADO.NET providers. – Marc Gravell Feb 03 '14 at 08:10
  • how to post my c# code – user3260112 Feb 03 '14 at 08:11
  • 3
    @user3260112 Select all your code with mouse. Right click the mouse and left click to `"Copy"` part, then come to your question. Click `"edit"` button under the question, there will be an area of your question. Right click there with mouse, then left click to `"Paste"` part. – Soner Gönül Feb 03 '14 at 08:14
  • plz any one help me , how to pass parameters. – user3260112 Feb 03 '14 at 08:24
  • The variable `@curRow` is a user defined variable in MySQL, the problem in your code is that Mysqlcommand treats it as a parameter, so it expects you to pass a variable to it, see [**this post**](http://stackoverflow.com/questions/958953/is-it-possible-to-use-a-mysql-user-defined-variable-in-a-net-mysqlcommand), it says that there was no way to solve it using the Mysql connect dot net. – Mahmoud Gamal Feb 03 '14 at 08:29
  • Try to add `Allow User Variables=True` to the connection string, see this http://blog.tjitjing.com/index.php/2009/05/mysqldatamysqlclientmysqlexception-parameter-id-must-be-defined.html – Mahmoud Gamal Feb 03 '14 at 08:30
  • possible duplicate of [How can I use a MySql User Defined Variable in a .NET MySqlCommand?](http://stackoverflow.com/questions/5524632/how-can-i-use-a-mysql-user-defined-variable-in-a-net-mysqlcommand) – Mahmoud Gamal Feb 03 '14 at 08:32
  • but here am getting connection string from web.config how can i add Allow User Variables=True; – user3260112 Feb 03 '14 at 09:08

0 Answers0