I'm getting the exception:
Error in Like operator: the string pattern 'Blah [Blah Blah]' is invalid.
It's occurring where I'm using a datatable's Select
method like so:
DataRow[] rows = table.Select("X Like '" + y + "'");
I've seen from reading other posts that it's the square brackets. The posts I've read say to escape the squares brackets but most of them focus on the source of the problem being a hard coded string the developer can easily escape.
I'm reading the data in from a database. I could do something like y.replace("[", "\\[")
which I'm guessing would fix the issue for square brackets. But then I'm wondering, what if elsewhere in the DB there is a string with an * etc.
How do others deal with this? Can I call something that will make a string safe by escaping all possibly illegal characters or is it a case of manually replacing them with escaped equivalents?