?
Solved

Escaping single quote in Datarow fileters

Posted on 2004-04-07
10
Medium Priority
?
1,217 Views
Last Modified: 2012-05-04
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);
0
Comment
Question by:saturn_one
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10779302
use a double '' to escape it
0
 

Author Comment

by:saturn_one
ID: 10779417
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);
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10779512


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"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:_TAD_
ID: 10779516


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

you need to remove the @ symbol all together.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10779517
string test = "Title= \'" + myCriteria + "\'"

0
 

Author Comment

by:saturn_one
ID: 10779533
actually i tried that too!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10779747

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

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:
0
 

Author Comment

by:saturn_one
ID: 10779833
actually my example is

"Jone's book"
rather than
'Jone''s'

i'm trying to escape a single quote in middle of a string!
 
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 160 total points
ID: 10779892
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 # #.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10779903
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)
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question