1

I am trying to make this work but have been unlucky it is giving me th following error

Error in Like operator: the string pattern '%testing : | / - “ ‘ & * # @%' is invalid.

This search works as long as it doesnt contain a string as the one above.. This is my code for the search

DataRow[] rows = GetAllItems.Select("Name like '%" + cleanedText + "%'");

I tried - Modified per cHaos (still errors though)

  string cleanedText =  SearchText.Replace("\"", "\\\"").Replace("'", "''");

but no luck when i enter the following string in the search although i know it is in the data

testing : | / - “ ‘ & * # @%

Anyone has a nice suggestion

Thank you

7
  • Are you using a SQL database? Commented Oct 13, 2011 at 19:34
  • Ummm... what does this do? sSearctText.Replace("\"", "\"")? Commented Oct 13, 2011 at 19:34
  • 1
    You do realize that .Replace("\"", "\"") does nothing useful, right? Same with all the other backslashed replacements -- you're replacing wacky chars with the exact same char. You probably want .Replace("\"", "\\\"") and the like. Commented Oct 13, 2011 at 19:35
  • sSearchText is the original variable - cHao, i used that Replace("\"", "\"") and it actually fixed the issue of not finding items with " in it.. dunno why, but i see your point Commented Oct 13, 2011 at 19:38
  • @cHao & Mark I changed sSearctText.Replace("\"", "\"")? to sSearctText.Replace("\"", "\\\"")? and still the same error. Commented Oct 13, 2011 at 19:45

1 Answer 1

3

Both % and * can be used interchangeably for wildcard characters in the LIKE comparison.

As such you will need to enclose these characters in square brackets in your expression string like so:

Name like '%testing : | / - “ ‘ & [*] # @%'

See DataColumn.Expression.

From the above link:

Wildcard Characters

Both the * and % can be used interchangeably for wildcard characters in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be enclosed in brackets ([]). If a bracket is in the clause, each bracket character should be enclosed in brackets (for example [[] or []]).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.