czchlong
czchlong

Reputation: 2594

Debugging yields incorrect syntax near keyword 'where'

I have the following function that I am trying to run that joins a product table and a productcategory table and return the results. The debugger, when exception is thrown, displays the query string and I have tried running the query string in my test mysql database and it works.

The CategorizedProduct is a model. I have checked and made sure that all the fields in the select list match the fields in the model.

I cannot find where the error is.

public static List<CategorizedProduct> GetAllCategorizedProducts(string category)
{
    using (var conn = Sql.GetConnection())
    {
        string queryString = "select ProductID,ProductName,ProductType,ProductCategory,CategoryName,Unit,PricePerUnit,IsAvailable,NumberOfUnits,RemainingUnits,WeightPerUnit" +
            " from product inner join productcategory where product.ProductCategory = productcategory.CategoryID and CategoryName = \'" + category + "\' order by ProductType;";
        return conn.Query<CategorizedProduct>(queryString).ToList();
    }
}

Upvotes: 0

Views: 49

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270391

Try this version instead:

select ProductID, ProductName, ProductType, ProductCategory,
       CategoryName, Unit, PricePerUnit, IsAvailable, NumberOfUnits,
       RemainingUnits, WeightPerUnit
from product inner join
     productcategory 
     on product.ProductCategory = productcategory.CategoryID 
where CategoryName = \'" + category + "\'
order by ProductType;

I do think that the on is optional in MySQL after join, but this might fix your problem. The on is required by any other database (and I think if you have certain ANSI settings on in MySQL).

Upvotes: 1

Related Questions