3

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);

4 Answers 4

7
        + "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.
Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

If they value is a string they are.
Had to mark back up since actually the poster is correct and Mr Stevenson-Leggett is mistaken
That is an important point about parameters. You don't have to worry about encapsulating strings or formatting dates and all that.
0

like he said

+ "lineName = '@productLine' " 

should be

+ "lineName = @productLine " 

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.