0

When I type ' in my TextBox, it's resulting an error. I know that because the ' is part of SQL Query. How do I avoid that? I've done it with Parameter, but didn't even work.

    private void theFilter(string FilterValue) {
        string thisQuery = "SELECT * FROM [Customer] WHERE CONCAT([Name], [Address], [Discount]) LIKE '%" + @FilterValue + "%'";
        using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
        using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) {
            thisSqlCommand.Parameters.AddWithValue("@FilterValue", FilterValue);
            using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
            using(DataTable thisDataTable = new DataTable()) {
                thisSqlDataAdapter.Fill(thisDataTable);
                DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
            }
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Lem Bidi
  • 63
  • 9

2 Answers2

3

Try using this one

private void theFilter(string FilterValue) {
    string thisQuery = "SELECT * FROM [Customer] WHERE CONCAT([Name], [Address], [Discount]) LIKE @FilterValue";
    using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
    using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) {
        thisSqlCommand.Parameters.AddWithValue("@FilterValue", "%" + FilterValue + "%");
        using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
        using(DataTable thisDataTable = new DataTable()) {
            thisSqlDataAdapter.Fill(thisDataTable);
            DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
        }
    }
}

Remember that you are using strings in parameters, not variables. You have to indicate the '%' in the AddWithValue method to be able to use wildcards in your query.

Errol
  • 46
  • 2
3
private void theFilter(string FilterValue) {
    string thisQuery = "SELECT * FROM [Customer] WHERE CONCAT([Name], [Address], [Discount]) LIKE @FilterValue";
    using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
    using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) {
        thisSqlCommand.Parameters.AddWithValue("@FilterValue", "%" + SqlLikeEscape(FilterValue) + "%");
        using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
        using(DataTable thisDataTable = new DataTable()) {
            thisSqlDataAdapter.Fill(thisDataTable);
            DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
        }
    }
}

// This function is important, since otherwise if there is a % in the table (or other special LIKE characters) then it is hard to search for them
//see https://stackoverflow.com/questions/18693349/how-do-i-find-with-the-like-operator-in-sql-server
public static string SqlLikeEscape(string value)
{
    if (string.IsNullOrEmpty(value)) return value;
    return Regex.Replace(value, @"(?<ch>%|_|\[)", @"[${ch}]");
}
mjwills
  • 23,389
  • 6
  • 40
  • 63