Here is a typical select filter string that you might perform on a DataTable:

clients.Select(string.Format(" DisplayName = '{0}' ", ClientName));

However if ClientName in the above example contains an apostrophe the select will fail. A simple fix is to the use following code to escape it:

ClientName.Replace("'","''")

Which replaces a single apostrophe with two.

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Tuesday, October 30, 2007 9:48 AM | in C#

Comments

Gravatar
# re: Escaping DataTable select queries/filters
Posted by Stuart Bennett
on 1/15/2008 3:49 PM
Thanks for this tip which also works in VBS..

Was trying to get :

SELECT samAccountName, givenName, sn, AdsPath FROM 'LDAP:// DC=xx,DC=x,DC=com' WHERE samAccountName = 'xyz'

and if the samAccountName had an apostrophe in, it was failing with mismatched apostrophes.

Adding in

if instr(ac,"'")<>0 Then
ac=replace(ac,"'","''")
end if

Has fixed it.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 8 and 5 and type the answer here: