I have the following SQL
SELECT Equipment.* ,cast(EquipmentId as varchar(100)) + ' ' + EquipmentName as Name
FROM Equipment
WHERE Equipment.EquipmentCategoryID = @EquipmentCategoryID
AND (Equipment.EquipmentSubCategoryID = @EquipmentSubCategoryID
OR (@EquipmentSubCategoryID is null OR @EquipmentSubCategoryID = '' ))
In SQL Server it is behaving as expected. When @EquipmentCategoryId is 12 and @EquipmentSubCategoryID is null It returns all the values I want. And when @EquipmentCategoryId is 12 and @EquipmentSubCategoryID is another number it returns the smaller amount of rows which is also what I want.
In an ASP.NET dropdownlist in a GridView it is not behaving as expected, returning all the rows for each dropdownlist in the GridView, even though the @EquipmentSubCategoryID has different numbers, that work in SQL Server. The dropdownlist is bound like this.
protected void ServiceFormsGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField EquipmentCategoryIDHF = (HiddenField) e.Row.FindControl("EquipmentCategoryIDHF");
HiddenField EquipmentSubCategoryIDHF = (HiddenField) e.Row.FindControl("EquipmentSubCategoryIDHF");
DropDownList EquipmentDD = (DropDownList) e.Row.FindControl("EquipmentDD");
EquipmentDS.SelectParameters["EquipmentCategoryID"].DefaultValue = EquipmentCategoryIDHF.Value;
EquipmentDS.SelectParameters["EquipmentSubCategoryID"].DefaultValue = EquipmentSubCategoryIDHF.Value;
EquipmentDD.Items.Clear();
EquipmentDD.Items.Add(new ListItem());
EquipmentDD.DataBind();
}
}
I can confirm that each time through the method that the HiddenFields are displaying the correct values but the ServiceFormsGV GridView has all the rows in each DropDownList.
The GridView is as follows.
<asp:GridView ID="ServiceFormsGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ServiceFormID,EquipmentCategoryID1" DataSourceID="ServiceFormsDS" OnDataBound="ServiceFormsGV_DataBound" OnRowDataBound="ServiceFormsGV_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Machine Id">
<ItemTemplate>
<asp:DropDownList ID="EquipmentDD" runat="server" AutoPostBack="True" DataSourceID="EquipmentDS" DataTextField="EquipmentName" AppendDataBoundItems="True"
DataValueField="EquipmentID" OnSelectedIndexChanged="EquipmentDD_SelectedIndexChanged">
And in the SQLDataSource I have
CancelSelectOnNullParameter="False"
which is necessary.
I have tested the query in Configure Data Source and it behaves as expected.

@EquipmentSubCategoryIDis not NULL or blank string every time? Also I tried the query you shared in SSMS and I see that when@EquipmentSubCategoryIDis having a value the query does not return any data.