Escaping DataTable select queries/filters

by Brad 30. October 2007 09:48
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.

Tags:

C#

Comments

1/15/2008 3:49:55 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.

Stuart Bennett |

1/15/2009 3:49:07 PM #



Great tip, was pulling my hair out but you've just saved me several hours manual work Wink

Thanks
Dave

Dave |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar