0

I have a client who wants filtering done on their application. One page has 8 filters and the other has 19. The filters can be combined. Basically 2^nth. Not a problem at all until it gets a high number such as 19! That would be... 524288 different combinations? Maybe I am not thinking this correctly.

So, what is a way I can have this done without creating that much code. My thinking was originally done as: 1. Create the number of check boxes needed. 2. Use logic (case,if, else, etc.) on the checked changed event to give the sql command for the desired gridview look and pass it.

But this is an insane amount of coding and there must be a more simple way than putting flags or checking if it is checked or not on every combination.

I have it working for the 5 combinations they originally wanted but 19? I just can't think of a way to do it without lots of code. Maybe I am exhausted and there is a simple answer.

To clarify, lets go to the 8 possible combinations. Lets say they have a client who eats 8 different foods. 1. Eggs 2. Bacon 3. Ham 4. Toast 5. Bagel 6. OJ 7. Milk 8. Apples

So the filtering could be: Eggs Or Eggs and bacon Or Eggs and bacon and ham Or Eggs and bacon and ham and Toast

Or Bacon Or Bacon and ham Or Bacon and ham and Toast

Or Ham Or Ham and toast

Etc. etc. etc. The combinations go on.

I don't know how to do this without checked if each combination is selected or not. I have it working for a smaller amount of filtering, but when the number gets large, well, the permutation just explodes to the insane amount.

EDIT --- THIS IS HOW I GOT IT TO WORK FOR ANYONE ELSE IN THIS SITUATION

protected void Page_Load(object sender, EventArgs e)
{
    bindFilter();
}

protected void chkBoxFilters_SelectedIndexChanged(object sender, EventArgs e)
{
    this.bindFilter();
}
private void bindFilter()
{
    string constr = ConfigurationManager.ConnectionStrings["YOUR CONNECTION STRING HERE"].ConnectionString;
    string query =
       "YOUR SQL QUERY HERE";

    string condition = string.Empty;
    foreach (ListItem item in chkBoxFilters.Items)
    {
        condition += item.Selected ? string.Format("'{0}',", item.Value) : string.Empty;
    }

    if (!string.IsNullOrEmpty(condition))
    {
        condition = string.Format(" WHERE COLUMN_NAME IN ({0})", condition.Substring(0, condition.Length - 1));
    }

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query + condition))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                cmd.Connection = con;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GRIDVIEWID.DataSource = dt;
                    GRIDVIEWID.DataBind();
                }
            }
        }
    }
}

}

And, of course, make a checkbox list with autopostback set to true. This worked for me, if anyone else in the future runs into something similar. If you have more than one column you are looking for, then add an OR [ColumnName] IN ({0}) afterwards and it works as well. You can have this filter multiple tables as well. Sweet and simple.

  • 1
    how are these 19 filters related? How do you get 2^nth combinationss? We certainly miss clear picture from your description!! – techspider Aug 22 '16 at 18:10
  • Upvoted you and clarified a bit more. So this is for multiple filtering using checkboxes and they can have any combination of values. – Andrew Bringaze Aug 22 '16 at 18:14
  • 1
    Not sure, if I still understood correctly; You can use a multi-select list box, concatenate all selected values, pass them to DB, get data and bind to Grid – techspider Aug 22 '16 at 18:16
  • That was one way I knew how to do it, but the client demands check boxes. Why? I don't know. – Andrew Bringaze Aug 22 '16 at 18:17
  • 1
    use `CheckBoxList` and do the same logic on this list as specified in my previous comment – techspider Aug 22 '16 at 18:19
  • Wouldn't this still be an extremely large number? The total combinations being the amount to the nth? So for 8 numbers it would be 256 combinations? – Andrew Bringaze Aug 22 '16 at 18:21
  • 1
    not sure what you mean!! CheckBoxList is similar to multi select ListBox except the items are shown with CheckBox – techspider Aug 22 '16 at 18:22
  • 1
    Look at [this example](http://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c) – techspider Aug 22 '16 at 18:24
  • Looking into that, I updated my question again. – Andrew Bringaze Aug 22 '16 at 18:25
  • 1
    have you thought about using Linq? – Stef Geysels Aug 22 '16 at 18:35
  • 1
    Can you show us what your code looks like? If the user selects `Eggs` and `Bacon`, what would you do exactly with that information? The number of combinations may be overwhelming but your filter should probably only consider the 19 flags, not each possible combination. – ConnorsFan Aug 22 '16 at 19:21
  • I didn't post code because it was so basic that I am in the pseudo stage and just written some basic on notepad. I would assume you would have a checked_changed event and a postback. In the checked changed, you would say, If(chkBox.Eggs.Checked && !chkBoxBacon.Checked){//do the sql logic for getting only eggs}. You may get the idea from there. Each checked event would pull the appropriate sql. – Andrew Bringaze Aug 22 '16 at 19:44
  • I have this working now and will post my code once I proof over it and make sure there aren't any issues. It was actually quite simple. I could have done it a few different ways but the client asked for check boxes and thus this is it. – Andrew Bringaze Aug 22 '16 at 20:30

1 Answers1

1

I'm not sure if this will help or not, but I've had to handle advanced search pages before and went this route...

Build a page full of Checkboxes, get each Checkbox's Checked property and pass it to a SQL stored procedure (perhaps with a SqlDataSource control and ControlParameter that looks like this:

CREATE PROC usp_web_search
    @eggs       bit     = NULL,
    @bacon      bit     = NULL,
    @ham        bit     = NULL,
    @toast      bit     = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT      * 
    FROM        myTable a
    WHERE       (@eggs IS NULL OR (a.Eggs = @eggs))
                AND (@bacon IS NULL OR (a.Bacon = @bacon))
                AND (@ham IS NULL OR (a.Ham = @ham))
                AND (@toast IS NULL OR (a.Toast = @toast));
END;

You can make the WHERE logic more complicated, if necessary, but this handles the variety of combinations. Any of the parameters could be NULL (they'll be ignored in the query), 0 (not checked; will be part of query), or 1 (checked; will be part of query).

Hope that's helpful.

scolja
  • 433
  • 4
  • 14
  • This might actually work. I will write some code out and test. If it does I will let you know and give appropriate credit. – Andrew Bringaze Aug 22 '16 at 19:46
  • I came up with a different solution. But what you gave also would work, maybe even better so. Since it does work, I am marking as the solution. Even though I did it another way. – Andrew Bringaze Aug 22 '16 at 20:45