Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
That seems to have worked, but I have no idea why it does. –  Nathanus Feb 9 '12 at 22:50
1  
Because in SQL, a single quote is the escape character for a single quote. –  blah238 Feb 9 '12 at 23:03
    
That's hilariously evil. –  Nathanus Feb 9 '12 at 23:25
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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