using singe qoutation in DataView.RowFilter

  • Thread starter Thread starter newbie_csharp
  • Start date Start date
N

newbie_csharp

Hi,
I need to use single qoutation ( ' ) in my filter string but I get error
message. how can I do something like this:

mysearch = "Bob's book";
dataView.RowFilter = "myfield LIKE '" + mysearch + "%'";

even I changed "mysearch to this but still it's not working:

mysearch = "Bob\'s book";

thanks
 
Newbie,
You need to use two single quotation marks in the "mysearch" variable:

Something like
mysearch = "Bob''s book";

That's: B, o, b, single quote, single quote, s ...

Hope this helps
Jay
 
newbie_csharp,

I believe you will have to double up on the quotes, like this:

// Alter the string so that it can be included in the filter.
filterString = mysearch.Replace(@"'", @"''");

An then search on that.

I personally don't like this, and have suggested a number of times to
people at MS that they provide something like parameters for this kind of
operation (so that we could pass CLR types and not have to worry about
conversions like this).

Hope this helps.
 
Nicholas,
Parameters or even simply an Encode/Escape function (like Regex.Escape &
Regex.Unescape) that properly encodes the string so it works correctly as a
"quoted" string in the filter.

One that properly, quotes: single quote, double quote, square bracket &
other characters to be used in the Filter & other properties of the DataSet
Object Model.

Hope this helps
Jay

Nicholas Paldino said:
newbie_csharp,

I believe you will have to double up on the quotes, like this:

// Alter the string so that it can be included in the filter.
filterString = mysearch.Replace(@"'", @"''");

An then search on that.

I personally don't like this, and have suggested a number of times to
people at MS that they provide something like parameters for this kind of
operation (so that we could pass CLR types and not have to worry about
conversions like this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

newbie_csharp said:
Hi,
I need to use single qoutation ( ' ) in my filter string but I get error
message. how can I do something like this:

mysearch = "Bob's book";
dataView.RowFilter = "myfield LIKE '" + mysearch + "%'";

even I changed "mysearch to this but still it's not working:

mysearch = "Bob\'s book";

thanks
 
Jay,

I don't think that an escape function is enough, as it would still force
me to concatenate parts of my expression together, something I wouldn't have
to do for a command to a database.

And yes, anywhere the Expression property is relevant, and handles all
types in the framework that can be stored in a dataset.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jay B. Harlow said:
Nicholas,
Parameters or even simply an Encode/Escape function (like Regex.Escape &
Regex.Unescape) that properly encodes the string so it works correctly as
a "quoted" string in the filter.

One that properly, quotes: single quote, double quote, square bracket &
other characters to be used in the Filter & other properties of the
DataSet Object Model.

Hope this helps
Jay

Nicholas Paldino said:
newbie_csharp,

I believe you will have to double up on the quotes, like this:

// Alter the string so that it can be included in the filter.
filterString = mysearch.Replace(@"'", @"''");

An then search on that.

I personally don't like this, and have suggested a number of times to
people at MS that they provide something like parameters for this kind of
operation (so that we could pass CLR types and not have to worry about
conversions like this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

newbie_csharp said:
Hi,
I need to use single qoutation ( ' ) in my filter string but I get error
message. how can I do something like this:

mysearch = "Bob's book";
dataView.RowFilter = "myfield LIKE '" + mysearch + "%'";

even I changed "mysearch to this but still it's not working:

mysearch = "Bob\'s book";

thanks
 
Nicholas,
The escape function itself could do the concatenation, by accepting a format
string & params args, similar to String.Format.

Something like:
mysearch = "Bob's book";
dataView.RowFilter = Escape("myfield LIKE '{0}%'", mysearch);

Otherwise I totally agree, a full parameters collection would be better!

Jay



Nicholas Paldino said:
Jay,

I don't think that an escape function is enough, as it would still
force me to concatenate parts of my expression together, something I
wouldn't have to do for a command to a database.

And yes, anywhere the Expression property is relevant, and handles all
types in the framework that can be stored in a dataset.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jay B. Harlow said:
Nicholas,
Parameters or even simply an Encode/Escape function (like Regex.Escape &
Regex.Unescape) that properly encodes the string so it works correctly as
a "quoted" string in the filter.

One that properly, quotes: single quote, double quote, square bracket &
other characters to be used in the Filter & other properties of the
DataSet Object Model.

Hope this helps
Jay

Nicholas Paldino said:
newbie_csharp,

I believe you will have to double up on the quotes, like this:

// Alter the string so that it can be included in the filter.
filterString = mysearch.Replace(@"'", @"''");

An then search on that.

I personally don't like this, and have suggested a number of times to
people at MS that they provide something like parameters for this kind
of operation (so that we could pass CLR types and not have to worry
about conversions like this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I need to use single qoutation ( ' ) in my filter string but I get
error
message. how can I do something like this:

mysearch = "Bob's book";
dataView.RowFilter = "myfield LIKE '" + mysearch + "%'";

even I changed "mysearch to this but still it's not working:

mysearch = "Bob\'s book";

thanks
 
Jay:

Shouldn't you be paying attention to the DevCon speaker instead of
hanging out in a newsgroup? :)
 
Scott,
?

I'm not at DevCon, so how would I be able to pay attention to the speaker?

Jay
 
Back
Top