I'm trying to query values using a cursor and loop for the process of removing diacritics. The problem I am running in to is that when I try to create a WhereClause like so...

qf.WhereClause = "\""+fieldname+"\" = "+"'"+address+"'";

I get a COMException about my query if the value of address has a single quote in it (i.e. Land o'Lakes). I have tried escaping the ' with address.Replace("'","\'") but the resulting string is unchanged. When I attempted to double-escape with address.Replace("'","\\'") , the string ends up with both backslashes.

What should I do?


2 Answers 2


I think it might matter what type of data source you are hitting but in general I suspect that doubling the single quote marks is the solution. So address.Replace( "'", "''"); should work.

  • That seems to have worked, but I have no idea why it does.
    – Nathanus
    Feb 9, 2012 at 22:50
  • 2
    Because in SQL, a single quote is the escape character for a single quote.
    – blah238
    Feb 9, 2012 at 23:03
  • That's hilariously evil.
    – Nathanus
    Feb 9, 2012 at 23:25

You can also use ASCII Character Codes by using the Char() function. The ASCII code for single quote is 39 and the code for double quote is 34. Using Char(39) will return '.

So you could say:

qf.WhereClause = "\""+fieldname+"\" = "+Char(39)+address+Char(39);

Here is a link to a list of ASCII codes: http://yorktown.cbe.wwu.edu/sandvig/shared/ASCIICodes.aspx

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.