Shkupjane
Shkupjane

Reputation: 53

Using where clause with join in ASP.NET

im receiving syntax error on it, getting problem while working with join in C#? Here is my code:

DataTable dtProducts = system.GetDataTable ("SELECT p.*, c.CategoryName, sc.CategoryName as SubCategoryName
                                             FROM TBLPRODUCTS p 
                                             LEFT JOIN TBLCATEGORIES c ON p.CategoryId = c.CategoryId 
                                             LEFT JOIN TBLCATEGORIES sc ON c.SubCategoryId = sc.CateogryId
                                             WHERE p.ProductID == ProductID");

Upvotes: 2

Views: 323

Answers (2)

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

You repeated the WHERE clause two times

 where 
     where p.ProductID == ProductID

Plus you should use = instead of == in this WHERE clause.

Finally you should add the @ symbol in front of a string to form a verbatim string literal:

It should read:

DataTable dtProducts = system.GetDataTable (@"Select 
        p.*, c.CategoryName, sc.CategoryName as SubCategoryName
    from 
        TBLPRODUCTS p 
    left join
        TBLCATEGORIES c on p.CategoryId = c.CategoryId 
    left join
        TBLCATEGORIES sc on c.SubCategoryId = sc.CateogryId
    where 
        p.ProductID = ProductID");

Upvotes: 1

Shukri Gashi
Shukri Gashi

Reputation: 535

I think you should use only one time equal sign

where p.ProductID == ProductID

change to

where p.ProductID = ProductID

also remove second where

Upvotes: 2

Related Questions