Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Tip/Trick

Winforms BindingSource Does Not Support Parameterized Queries - So Your App Crashes When Your User Types "doesn't"

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
31 Jul 2018CPOL 8.8K   5   2
Winforms BindingSource does not support Parameterized Queries - so your app crashes when your user types "doesn't"

Introduction

The Winforms BindingControl is very useful to filter DataGridView and similar controls quickly, because it has a Filter property which allows the display data to change without modification of the underlying data source. But ... it's a string. And there is no "Parameters" collection.

So you can't pass your user input as a parameterized query, you have to use string concatenation:

C#
dataSource.Filter = string.Format("description LIKE '%{0}%' OR FullText LIKE '%{0}%'", s);

And that means that if your user enters "doesn't work" or "don't do it!" or "100% or more" then your app crashes (you can catch the exception, yes - but that doesn't help your user much) because the filter sees the string:

description LIKE '%doesn't%' OR FullText LIKE '%doesn't%'

and doesn't know what to do with it.

Background

The solution - or at least the only solution I've found - is to escape the "dodgy" characters in the input string:

'*', '%', '[', and ']' need to be surrounded by square brackets

'\'' needs to be replaced with a pair of single quotes.

Simple enough, so I knocked up an extension method:

C#
using System.Diagnostics;
using System.Text;

namespace GeneralTesting
    {
    public static class ExtensionMethods
        {
        private static string[] replace = { "*", "%", "[", "]", "'" };
        private static string[] with = { "[*]", "[%]", "[[]", "[]]", "''" };
        /// <summary>
        /// Escapes special characters for LIKE queries
        /// </summary>
        /// <remarks>
        /// BindingSource doesn't accept parameterized queries: so we have to
        /// manually "fix" user input to prevent "doesn't", "don't", and so forth
        /// crashing the app.
        /// </remarks>
        /// <param name="raw"></param>
        /// <returns></returns>
        public static string EscapeLikeValue(this string raw)
            {
            Debug.Assert(replace.Length == with.Length, "replace and with do not match");
            StringBuilder sb = new StringBuilder(raw.Length * 3);
            sb.Append(raw);
            for (int i = 0; i < replace.Length; i++)
                {
                sb.Replace(replace[i], with[i]);
                }
            return sb.ToString();
            }
        }
    }

Using the Code

Simple:

C#
/// <summary>
/// Filter changed - apply it.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbFilter_TextChanged(object sender, EventArgs e)
    {
    string s = tbFilter.Text;
    if (!string.IsNullOrWhiteSpace(s))
        {
        s = s.EscapeLikeValue();
        dataSource.Filter = string.Format("description LIKE '%{0}%' OR FullText LIKE '%{0}%'", s);
        }
    else
        {
        dataSource.Filter = null;
        }
    }

Points of Interest

I learned that Microsoft doesn't think ahead and too many of its programmers don't understand parameterized queries, which is both sad and worrying...

History

  • 2018-08-01: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
SuggestionIt's not an issue Pin
vbjay.net3-Aug-18 1:51
vbjay.net3-Aug-18 1:51 
We have LINQ. I haven't used filter in a long time. Why use a limited SQL esque language when linq is available?
Bugsquare bracket problem Pin
sx20082-Aug-18 8:42
sx20082-Aug-18 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.