I have a .NET C# webform. The CodeBehind picks up some textbox entry to append to a select statement for the txtboxes that are used. Here are the ASP textboxes:
<asp:TextBox ID="txtBadge" runat="server" CssClass="txtBadgeStyle"></asp:TextBox>
<asp:TextBox ID="txtFirst" runat="server" CssClass="txtFirstStyle"></asp:TextBox>
<asp:TextBox ID="txtLast" runat="server" CssClass="txtLastStyle"></asp:TextBox>
Here's the (current wrong version) server side:
StringBuilder sbCommand = new StringBuilder("SELECT a.BADGE_ID, a.FIRSTNAME, a.LASTNAME, a.TITLE, b.deptname, CASE WHEN a.ACTIVE=1 THEN 'Yes' ELSE 'No' END FROM XA_EMPMAS a left join dept_master b on b.deptnum = a.deptnum where 1 = 1");
if (string.IsNullOrEmpty(txtBadge.Text) == false)
{
sbCommand.Append(" AND a.BADGE_ID=@txtBadge");
SqlParameter param = new SqlParameter("@txtBadge", txtBadge.Text);
cmd.Parameters.Add(param);
}
if (string.IsNullOrEmpty(txtFirst.Text) == false)
{
sbCommand.Append(" AND a.FIRSTNAME=@txtFirst");
SqlParameter param = new SqlParameter("@txtFirst", txtFirst.Text);
cmd.Parameters.Add(param);
}
if (string.IsNullOrEmpty(txtLast.Text) == false)
{
sbCommand.Append(" AND a.LASTNAME like " + @txtLast + "%");
SqlParameter param = new SqlParameter("@txtLast", txtLast.Text);
cmd.Parameters.Add(param);
}
So I want to convert the first name and last name appends to LIKE instead of EQUALS. Leaving FIRSTNAME alone for now, I've been fooling around with LASTNAME and haven't had any success.
Can someone give me the syntax correction for using like in the append command please?
sbCommand.Append(" AND a.LASTNAME like " + @txtLast + "%");
Thanks