SELECT *
FROM [productDetail]
WHERE
Price BETWEEN 0 AND 2000
OR Price BETWEEN 2000 AND 12000
AND CategoryID = 2
AND caravanOffline = 0
ORDER BY
price
The query doesn’t produce the desired output, it also shows the products whose categoryID is 3,1 etc.
Can any one spot the problem in the query.
Updated code:
static public DataTable GetSelectedFilterMotorhomes(ArrayList caravans)
{
string sqldefault = "Select * from productDetail Where";
string sqlBuilder = "";
int starter = 0;
int count = caravans.Count;
string sqlOperator = "OR";
if (caravans.Count > 0)
{
while (count > starter) //build the query depending on the number of caravans.selected.count
{
sqlBuilder += "((Price between " + caravans[count - 1] + "))" + sqlOperator;
count--;
}
string addQuery = sqldefault + sqlBuilder;
string removeOR = addQuery.Substring(0, addQuery.Length - 2); //remove OR
string finalQuery = removeOR + "AND (CategoryID = 2) AND (caravanOffline = 0) order by Price"; //Display caravans in ascending order
SqlDataAdapter da = new SqlDataAdapter(finalQuery, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
else
{
string returnAllCaravans = "Select * from productDetail WHERE CategoryID = 2 AND caravanOffline = 0";
SqlDataAdapter da = new SqlDataAdapter(returnAllCaravans, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
What do you really want to do with your WHERE clause?? Without any parenthesis, it’s not clear at all what you’re trying to select
DO you really mean:
Price is between 0 and 2000, or between 2000 and 12000 (doesn’t really make sense), AND in any case, the
CategoryIDis 2 ?Or do you mean:
The price is either between 0 and 2000 (with no other restriction) OR the price is between 2000 and 12000, but only if the
CategoryID = 2andCaravanOffline = 0are true at the same time) ??Or are you looking for something else than these two options??