I have a dropdown list ddlProjecsFilter
that I populate with the names of all projects in my database.
On page load, I show this dropdown list and when a person selects a Name, they get all the information regarding that Project Name. However, I have two extra filters based on character input (Funder, Type).
When writing the query like this, the dropdown list has no elements in it, yet the two other filters work.
protected void nameFilter()
{
string queryDefault = "SELECT NULL AS [Value], 'All' AS [Text], '0' AS [OrderNo] UNION ALL ";
string queryBase = "SELECT [NameId] AS [Value], [Type] AS [Text], '1' AS [OrderNo] FROM [tblProjects] ";
string queryWhere = " WHERE NameId IS NOT NULL ";
string queryOrder = " ORDER BY [OrderNo], [Text]";
if (!string.IsNullOrEmpty(txtTypeFilter.Text))
{
queryWhere +=" AND [Funder] LIKE @Funder";
queryWhere += " AND [Type] LIKE @Type";
}
sdsProjects.SelectCommand = queryDefault + queryBase + queryWhere + queryOrder;
sdsProjects.SelectParameters.Add("@Funder", txtFunderFilter.Text);
sdsProjects.SelectParameters.Add("@Type", txtTypeFilter.Text);
ddlProjectsFilter.DataBind();
}
If I however do it like this, everything works as intended. However, this is not best practice.
if (!string.IsNullOrEmpty(txtTypeFilter.Text))
{
queryWhere += string.Format(" AND [Funder] LIKE'%{0}%'", txtFunderFilter.Text);
queryWhere += string.Format(" AND [Type] LIKE'%{0}%'", txtTypeFilter.Text);
}
Can anyone point out the issue with the first snippet?
Definitions:
<td>
<asp:DropDownList runat="server" DataSourceID="sdsProjects" ID="ddlProjectsFilter"
AutoPostBack="true" DataTextField="Text" DataValueField="Value"
CssClass="form-control local-control" OnSelectedIndexChanged="ddlProjectsFilter_SelectedIndexChanged" AppendDataBoundItems="true">
</asp:DropDownList><asp:SqlDataSource ID="sdsProjectsFilter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseLive %>"></asp:SqlDataSource>
</td>
<td></td>
<td>
<asp:TextBox runat="server" ID="txtTypeFilter" AutoPostBack="true" OnTextChanged="txtTypeFilter_TextChanged"
CssClass="form-control local-control TypeFilter"></asp:TextBox></td>
<td></td>
<td>
<asp:TextBox runat="server" ID="txtFunderFilter" AutoPostBack="true" OnTextChanged="txtFunderFilter_TextChanged"
CssClass="form-control local-control FunderFilter"></asp:TextBox></td>...