Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi I am into a homefinder project that should have an advanced search filter. It should have 3 dropdownlist from where the user can select rent amount, location, and gender type or the user can also Not select any bcause it has default value "ALL" which leads to showing all the houses stored n the database but if the user selects one or two values from the dropdownlist with the other default value as "ALL", or all the three has values, it should lead to the union of them.

What I have tried:

I tried using if statements
C#
if (combobox1.text == "ALL" && combobox2.text == "ALL" && combobox3.text == "ALL")
{
 // shows all the homes
}
else if ( combobox1.text != "ALL" && combobox2.text == "ALL" && combobox3.text == "ALL")
{
 // say combobox1 is rent amount, shows all the homes having that rent amount
}

else if (combobox1.text == "ALL" && combobox2.text != "ALL" && combobox3.text == "ALL")
{
   // say combobox2 is gender type shows all the homes having that        gender category
   if (combox2.text == "Female")
     // all homes w "female"
   else
     // all homes w male
}

else if ( combobox1.text == "ALL" && combobox2.text == "ALL" && combobox3.text != "ALL")
{
// say combobox3 is location, shows all homes in that location
}

else if ( combobox1.text != "ALL" && combobox2.text != "ALL" && combobox3.text != "ALL")
{
}
else if ( combobox1.text != "ALL" && combobox2.text != "ALL" && combobox3.text == "ALL")
{
}

and so on, this is the code I have thought so far :l how can I make the intersection of them. Like if I choose 500 under rentamount and 1st Street under location, how can I find the homes with 500 as rent amount that is located in the 1st Street?


Any help from you is appreciated. Thank you.
Posted
Updated 27-Apr-16 11:51am
v2
Comments
Matt T Heffron 27-Apr-16 17:02pm    
At the top you say "union" of the conditions, and at the bottom you say "intersection". Which is it?
Sergey Alexandrovich Kryukov 27-Apr-16 17:20pm    
The inquirer thinks he answered you. :-)
—SA
[no name] 27-Apr-16 17:12pm    
They're the same. Union of the items in dropdownlist or their intersection of values. To avoid confusion, let's just focus on the union ^^
Sergey Alexandrovich Kryukov 27-Apr-16 17:20pm    
No. "Union" is disjunction, "intersection" is conjunction. Please...
—SA
Sergey Alexandrovich Kryukov 27-Apr-16 17:23pm    
Conclusion: the question is messy and pointless; it makes no sense without knowing what's the data layer. Nothing is tried or the try is not shown. The shown code is unrelated to it, at best, can be used as the illustration for the formulation; everything is hard-coded, so you could be not any really usable fragment...
—SA

1 solution

Assuming that you have a SqlCommand object which I will call sqlCommand and your 3 badly named comboBoxes are in order c1 - rent, c2 - gender, c3 - location (I'm using the shorthand because I could not be bothered to create 3 comboBoxes on my sandbox form)

Firstly you need to know which, if any, of the comboBoxes are set to the default of "ALL"...
C#
var b1 = (c1 == "ALL");
var b2 = (c2 == "ALL");
var b3 = (c3 == "ALL");
These Booleans help to control what you are going to put into your WHERE clause. Because we are going to "add" to the where clause dynamically and because strings are immutable, use a stringBuilder to generate the query...
C#
var whereQuery = new StringBuilder(" WHERE ");

Then add the sections into the WHERE clause based on the Boolean values...
C#
if (b1)
{
    whereQuery.Append("rentColum = @rent");
    sqlCommand.Parameters.AddWithValue("@rent", c1);
}
if (b2)
{
    whereQuery.Append((b1) ? " AND " : "");
    whereQuery.Append("genderColumn = @gender");
    sqlCommand.Parameters.AddWithValue("@gender", c2);
}
if (b3)
{
    whereQuery.Append((b1 || b2) ? " AND " : "");
    whereQuery.Append("locationColumn = @location");
    sqlCommand.Parameters.AddWithValue("@location", c3);
}

Note that I'm building a parameterised query. Also note the way that " AND " is only added if there has been a filter added prior to c2, c3, (etc)

When you have finished building your where clause then add it to your SELECT query
selectQuery += whereQuery.ToString();

Please note, because of the lack of information in your question this answer is completely untested. There may be minor typing errors.
 
Share this answer
 
Comments
[no name] 27-Apr-16 20:23pm    
Okay my bad. But thank you for your help. I am considering this solution u gave. I will try this when I code.
BillWoodruff 27-Apr-16 23:41pm    
See my new comment on your post.
CHill60 28-Apr-16 6:57am    
Let me (us) know if it doesn't work and I'll try to help further.
BillWoodruff 27-Apr-16 23:41pm    
+5
CHill60 28-Apr-16 6:57am    
Thank you ... and for the voice of reason in the comments earlier too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900