PC Review


Reply
Thread Tools Rate Thread

Escaping a single quote in DataTable select expresssion

 
 
=?Utf-8?B?U2lyIFBoaWxsaXAgU3lkbmV5?=
Guest
Posts: n/a
 
      12th Sep 2004
I am adding data to a DateTable in a DataSet and need to verify if a row
already exists with a specific column value:

string aName = "O'Connor";
string filter = "Name='" + aName + "'";
DataRow [] drs = MyDataset.MyTable.Select(filter);
if( drs.Length == 0 ) {
// Add a row for this name
}

Note the name (O'Connor) contains a single quote.

How should I escape the aName value so the filter works?

Disclaimers:
1. This is a select on a DataTable not a database select -- so no
parameterized queries.
2. The table MyDataset.MyTable already has a primary key on another field,
so I can't do a MyTable.FindByXXX

 
Reply With Quote
 
 
 
 
Val Mazur
Guest
Posts: n/a
 
      12th Sep 2004
Hi,

You need to double it and it should work

--
Val Mazur
Microsoft MVP


"Sir Phillip Sydney" <(E-Mail Removed)> wrote in
message news:2933F6D1-223B-4AC4-A995-(E-Mail Removed)...
>I am adding data to a DateTable in a DataSet and need to verify if a row
> already exists with a specific column value:
>
> string aName = "O'Connor";
> string filter = "Name='" + aName + "'";
> DataRow [] drs = MyDataset.MyTable.Select(filter);
> if( drs.Length == 0 ) {
> // Add a row for this name
> }
>
> Note the name (O'Connor) contains a single quote.
>
> How should I escape the aName value so the filter works?
>
> Disclaimers:
> 1. This is a select on a DataTable not a database select -- so no
> parameterized queries.
> 2. The table MyDataset.MyTable already has a primary key on another field,
> so I can't do a MyTable.FindByXXX
>



 
Reply With Quote
 
=?Utf-8?B?U2lyIFBoaWxsaXAgU3lkbmV5?=
Guest
Posts: n/a
 
      12th Sep 2004
Indeed it does. Thank you sir!

Strange this isn't mentioned in the DataColumn.Expression Property
documentation (at least, if it is, I can't find it) -- seems like pretty
fundamental information. All the information about escaping seems to relate
to how to escape wierd column names.

Any doc-person at Microsoft listening?

"Val Mazur" wrote:

> Hi,
>
> You need to double it and it should work
>
> --
> Val Mazur
> Microsoft MVP
>
>
> "Sir Phillip Sydney" <(E-Mail Removed)> wrote in
> message news:2933F6D1-223B-4AC4-A995-(E-Mail Removed)...
> >I am adding data to a DateTable in a DataSet and need to verify if a row
> > already exists with a specific column value:
> >
> > string aName = "O'Connor";
> > string filter = "Name='" + aName + "'";
> > DataRow [] drs = MyDataset.MyTable.Select(filter);
> > if( drs.Length == 0 ) {
> > // Add a row for this name
> > }
> >
> > Note the name (O'Connor) contains a single quote.
> >
> > How should I escape the aName value so the filter works?
> >
> > Disclaimers:
> > 1. This is a select on a DataTable not a database select -- so no
> > parameterized queries.
> > 2. The table MyDataset.MyTable already has a primary key on another field,
> > so I can't do a MyTable.FindByXXX
> >

>
>
>

 
Reply With Quote
 
Val Mazur
Guest
Posts: n/a
 
      13th Sep 2004
It is just some sort of standard. It was exact same way in ADO and it works
exact same way in a SQL statements. This is probably why Microsoft does not
mention it in documentation

--
Val Mazur
Microsoft MVP


"Sir Phillip Sydney" <(E-Mail Removed)> wrote in
message newsB68406D-3EE1-4672-9CE4-(E-Mail Removed)...
> Indeed it does. Thank you sir!
>
> Strange this isn't mentioned in the DataColumn.Expression Property
> documentation (at least, if it is, I can't find it) -- seems like pretty
> fundamental information. All the information about escaping seems to
> relate
> to how to escape wierd column names.
>
> Any doc-person at Microsoft listening?
>
> "Val Mazur" wrote:
>
>> Hi,
>>
>> You need to double it and it should work
>>
>> --
>> Val Mazur
>> Microsoft MVP
>>
>>
>> "Sir Phillip Sydney" <(E-Mail Removed)> wrote
>> in
>> message news:2933F6D1-223B-4AC4-A995-(E-Mail Removed)...
>> >I am adding data to a DateTable in a DataSet and need to verify if a row
>> > already exists with a specific column value:
>> >
>> > string aName = "O'Connor";
>> > string filter = "Name='" + aName + "'";
>> > DataRow [] drs = MyDataset.MyTable.Select(filter);
>> > if( drs.Length == 0 ) {
>> > // Add a row for this name
>> > }
>> >
>> > Note the name (O'Connor) contains a single quote.
>> >
>> > How should I escape the aName value so the filter works?
>> >
>> > Disclaimers:
>> > 1. This is a select on a DataTable not a database select -- so no
>> > parameterized queries.
>> > 2. The table MyDataset.MyTable already has a primary key on another
>> > field,
>> > so I can't do a MyTable.FindByXXX
>> >

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If single-quote and double-quote are in text Bob Barnes Microsoft Access 2 25th Nov 2007 10:56 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman Microsoft ASP .NET 3 14th Apr 2007 09:16 AM
How to "escape" a single quote in SQL Server CE select statement Malcolm McGrath Microsoft Dot NET Compact Framework 3 8th Apr 2005 04:21 AM
Escaping single quotes through ADO.NET Mitchell Vincent Microsoft VB .NET 4 18th Oct 2004 12:42 AM
Escaping single quote with bound control - how?? Mark Microsoft ADO .NET 2 13th Nov 2003 10:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:34 AM.