Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:12:20+00:00 2026-06-07T04:12:20+00:00

So, I have a GridView , CustomerGridView, and a SqlDataSource , SqlDataSource1, that pulls

  • 0

So, I have a GridView, “CustomerGridView,” and a SqlDataSource, “SqlDataSource1,” that pulls the data from my sql table. Now I wanted to add some filtering, so I created a side panel with various options.

My issue is that with my current code, if I select, say Customers that pay monthly, new customers, and also customers that pay semi-annually. It shows me all of these types including active (not new) customers that meet the criteria, instead of showing me only new customers that either pay semi-annually OR monthly.

How can I fix this?

I’ve tried parentheses and such, but all to no avail; would I just have to create an if for every situation and manually write each filterexpression?

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource1.FilterExpression = string.Empty;
    if (MonthlyCheckBox.Checked)
    {

            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Monthly'";
    }
    if (SemiAnnuallyCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += "OR ([CustomerSubscriptionType]='Semi-Annually')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Semi-Annually'";
        }

    }
    if (AnnuallyCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += "OR ([CustomerSubscriptionType]='Annually')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Annually'";
        }
    }
    if (NewCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " ( OR [CustomerStatus]='Active')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='New'";
        }

    }
    if (ActiveCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " OR [CustomerStatus]='Active'";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='Active'";
        }
    }
    if (SuspendedCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " OR [CustomerStatus]='Suspended'";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='Suspended'";
        }
    }
    if (ResidentialCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
                SqlDataSource1.FilterExpression += " AND [CustomerType]='Residential' ";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerType]='Residential'";
        } 
    }
    if (CommercialCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerType]='Commercial' ";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerType]='Commercial'";
        } 

    }
    if (NotInGroupCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]=''";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]=''";
        }
    }
    if (InGroupCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]<>''";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]<>''";
        }
    }
    if (GroupDropDownFilter.Text != "Select...")
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]='{0}'";
            SqlDataSource1.FilterParameters.Add("@CustomerGroup", GroupDropDownFilter.Text);
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]='{0}'";
            SqlDataSource1.FilterParameters.Add("@CustomerGroup", GroupDropDownFilter.Text);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:12:21+00:00Added an answer on June 7, 2026 at 4:12 am

    You’re using the OR operator.

    if(@CustomerSubscriptionType = 'Monthly' OR @CustomerStatus = 'New') will give you ALL customer’s that pay monthly (Active and New) and ALL New customers (regardless of subscription type).

    If you use the AND operator, you’ll ONLY get New customers paying Monthly.

    You’ll probably want to use a combination of the two operators, so I would use the OR operator within the group and use the AND operator between groups.

    if(
        (@CustomerSubscriptionType = 'Monthly' OR @CustomerSubscriptionType = 'Semi-Monthly')
    AND (@CustomerStatus = 'New')
    AND (@SomeOtherCriteria = 'Other' OR @SomeOtherCriteria = 'Other2')
    AND etc...
    

    To fix your code, I would use separate strings for each ‘group’:

    SqlDataSource1.FilterExpression = string.Empty;
    
    List<string> subscriptionTypeFilter = new List<string>();
    if (MonthlyCheckBox.Checked)
    {
        subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Monthly'");
    }
    
    if (SemiAnnuallyCheckBox.Checked)
    {
        subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Semi-Annually'");
    }
    
    if (AnnuallyCheckBox.Checked)
    {
    subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Annually'");
    }
    
    List<string> customerStatusFilter = new List<string>();
    if (NewCheckBox.Checked)
    {
    subscriptionTypeFilter.Add("[CustomerStatus]='New'");             
    }
    
    if (ActiveCheckBox.Checked)
    {
    subscriptionTypeFilter.Add("[CustomerStatus]='Active'");
    }
    
    if (SuspendedCheckBox.Checked)
    {
    subscriptionTypeFilter.Add("[CustomerStatus]='Suspended'");
    }
    
    
    List<string> filters = new List<string>();
    
    filters.Add("(" + string.Join(" OR ", subscriptionTypeFilter) + ")");
    filters.Add("(" + string.Join(" OR ", customerStatusFilter) + ")");
    
    SqlDataSource1.FilterExpression = string.Join(" AND ", filters);      
    

    EDIT: Yeah, this is what I meant by having “to create an if for every situation and manually write each filterexpression.” I was hoping I wouldn’t have to do that and could make it work some other way

    To do what you want dynamically, you could specify the field name and the value as custom attributes on the element:

    <asp:CheckBox id="SuspendedCheckBox" runat="server" fieldName="CustomerStatus" fieldValue="Suspended" />
    

    Once all the checkboxes have been defined properly, it’s just a matter of looping through the controls.

    foreach(Control c in this.Controls)
    {
        if(c is CheckBox)
        {
            CheckBox cb = (CheckBox)c;
    
            if(cb.IsChecked)
            {
                string fieldName = cb.Attributes["fieldName"];
                string fieldValue = cb.Attributes["fieldValue"];
    
                someDictionaryMaybe.Add(string.Format("[{0}] = '{1}'", fieldName, fieldValue), fieldName);
            }
        }
    }
    
    // loop through the dictionary, joining the different expressions with ORs and ANDs
    // or use LINQ to put it all together, it's up to you.  The fieldName was added to 
    // the dictionary, which can be used to separate with ANDs and ORs.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Gridview and I want to retrieve some Information from data base(SQL). and
I have GridView that allows select. It takes data from EntityDataSource . How do
I have a gridview and sqldatasource. I'm using : SqlDatasource1.SelectCommand = Select Name from
I have gridview, when I connect the sqldatasource and run the application, data from
I have a GridView using LinqDataSource to populate data from table Tickets . The
I have a GridView in android which I fill it with data retrieved from
Here i have gridview which contain some values retrieved from database.We put radiobutton list
I have a GridView and SqlDataSource , I have a column type: DateTime .
I have a gridview that needs to be exported to Excel. I have managed
I have a gridview and sqldatasource. In tabledefinition the default format for date is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.