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);
}
}
}
You’re using the
ORoperator.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
ANDoperator, 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
ORoperator within the group and use theANDoperator between groups.To fix your code, I would use separate strings for each ‘group’:
To do what you want dynamically, you could specify the field name and the value as custom attributes on the element:
Once all the checkboxes have been defined properly, it’s just a matter of looping through the controls.