tags:

views:

431

answers:

4

In the following code, used to get a list of products in a particular line, the command only returns results when I hard code (concatenate) productLine into the SQL. The parameter substitution never happens.

            + "lineName = '@productLine' "                       
            + "and isVisible = 1 ";
        MySqlDataAdapter adap = new MySqlDataAdapter(sql, msc);
        adap.SelectCommand.Parameters.Add("@productLine", productLine);
+2  A: 

Remove the apostrophes (spelling?). The ' around the parameter. They should not be needed.

Rune Grimstad
If they value is a string they are.
Rob Stevenson-Leggett
Had to mark back up since actually the poster is correct and Mr Stevenson-Leggett is mistaken
Robert
The answer is right. Rob makes a mistake in his comment.
Sunny
That is an important point about parameters. You don't have to worry about encapsulating strings or formatting dates and all that.
Rune Grimstad
A: 

like he said

+ "lineName = '@productLine' "

should be

+ "lineName = @productLine "
DrG
A: 

That's correct it never happens you have

  • "lineName = '@productLine' "

try

  • "lineName = @productLine " instead as @productLine will already be declared as a string type the quotes will be added secretly. You however are actually passing the string @productLine and not the variable value.
Robert
+2  A: 
        + "lineName = ?productLine "                       
        + "and isVisible = 1 ";
    MySqlDataAdapter adap = new MySqlDataAdapter(sql, msc);
    adap.SelectCommand.Parameters.Add("?productLine", productLine);
  1. Remove the apostrophes (').
  2. Change @ to ?, which is the prefix of parameters in MySql queries.
Omer van Kloeten
Thank you Mr. van Kloeten.
ProfK