• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1241
  • Last Modified:

Escaping single quote in Datarow fileters

I'm trying to use the select on my typed data set but i get an error when the string i'm looking for contains a single quote!

string FilterString="Title=  '"+ MyCriteria + "'";
int TotalFound =(MyDatasource.Table.Select(FilterString);
  • 4
  • 3
  • 3
1 Solution
use a double '' to escape it
saturn_oneAuthor Commented:
i tried but i don't know how to scape double quote! i tried this but doesn't work
string @test="Title=  " + Convert.ToChar(34)+ title + Convert.ToChar(34);

you need to use the back slash.  Also, your @ symbol is in the wrong place.'

string test = @"Title= \'" + myCriteria + "\'"

Use the same thing with double quotes.

Console.WriteLine("this is \"it\"");  //==>  this is "it"
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


actually... come to think of it... using the @ symbol negates the escape sequences.

you need to remove the @ symbol all together.
string test = "Title= \'" + myCriteria + "\'"

saturn_oneAuthor Commented:
actually i tried that too!

I believe you need to place double single quotes like in sql server ... sorry if I worded it incorrectly before ....

example 'Jone''s'

so the correct code is ...

string test = "Title= \'" + myCriteria.Replace("\'","\'\'") + "\'";

MSDN covers dates and escape sequences when used in column names (which you should also be careful of)

The following characters are special characters and must be escaped, as explained below, if they are to be used in a column name:

\n (newline)

\t (tab)

\r (carriage return)





















If a column name contains one of the above characters, the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write "[Column#]":


User-defined values may be used within expressions to be compared against column values. String values should be enclosed within single quotes. Date values should be enclosed within pound signs (#). Decimals and scientific notation are permissible for numeric values. For example:

"FirstName = 'John'"

"Price <= 50.00"

"Birthdate < #1/31/82#"

For columns that contain enumeration values, cast the value to an integer data type. For example:
saturn_oneAuthor Commented:
actually my example is

"Jone's book"
rather than

i'm trying to escape a single quote in middle of a string!
try this code instead of yours ...

string FilterString="Title=  \'" + myCriteria.Replace("\'","\'\'") + "\'";
int TotalFound =(MyDatasource.Table.Select(FilterString);

assuming that myCriteria is the string that you are comparing to title.

when you are applying an expression in a select rowfilter etc it is supposed to have string wrapped in SINGLE quotes as the included MSDN text says ....

Example ....
Title = 'jon'
Title = 'Jone''s Book'
Title = 'A Good Book'

you are not supposed to use double quotes within the user data side of the expression to delimit the string.

if you do not believe me I offer the following code ...

            static void Main(string[] args)
                  DataTable dt = new DataTable("test") ;
                  dt.Columns.Add("test1", typeof(System.String));
                  dt.Columns.Add("test2", typeof(System.String));
                  DataView foo = new DataView(dt);
                  foo.RowFilter = "test1 = \'jones\'\'\ssss'";

if you enclose it in double quotes or do not escape the single quote you will get a syntax exception on the line where it sets the rowfilter. rowfilter uses the same expression arguments as select().

on a side note I find it humorous that in MSDN under select() they actually use a date in the format '1/1/00' and under expression state it should be encapsulated with # #.
in case there was confusion judging from re-reading your last reply I am not using a double quote in the string ... I am using 2 single quotes. it must be replaced by 2 single quotes (the exact same operation you do with sql server if not using parameters)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now