Timothy Wong
Timothy Wong

Reputation: 95

Must declare the scalar variable "@ASIN"

I dont understand the error message. What does it mead by scalar variable? @ASIN value should be stored in the id variable I created for the GetByAsin method.

//Gets one Music Detail
public MusicDescriptionModel GetByAsin(string id)
{
    using (IDbConnection db = connection)
    {
        string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +
            "FROM tblDescription WHERE id = @ASIN";
        //need to parameterize ID to avoid sql injection attacks.
        MusicDescriptionModel musicdesc = db.Query<MusicDescriptionModel>(sql, new { id }).SingleOrDefault();
        return musicdesc;
    }
}

Upvotes: 0

Views: 790

Answers (3)

Timothy Wong
Timothy Wong

Reputation: 95

The problem was in my where statement I need to make the scalar variable @id WHERE ASIN = @id

Upvotes: 0

dteske
dteske

Reputation: 77

string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +  
                       "FROM tblDescription WHERE ASIN = @ASINSqlParam";
        //need to parameterize ID to avoid sql injection attacks.
        MusicDescriptionModel musicdesc = db.Query<MusicDescriptionModel>(sql, new { ASINSqlParam = id }).SingleOrDefault();

Should do the trick.

Docs here may help.

Upvotes: 0

mason
mason

Reputation: 32703

You haven't passed a variable for Dapper to bind to @ASIN. Try:

MusicDescriptionModel musicdesc
  = db.Query<MusicDescriptionModel>(sql, new { ASIN = id }).SingleOrDefault();

or try:

 string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +  
                       "FROM tblDescription WHERE id = @Id";

You can't use a different name and expect it to magically line them up for you. They need to match.

Upvotes: 3

Related Questions