DataView + RowFilter +Like %

M

marcin

Hello,


How can I use special characters % and * in DataView.RowFilter
expression in which I have already LIKE statement?


I am using a DataView to displaying a records from datatable, I allow
users to enter the string which will be pasted into RowFilter in such way

MyDataView.RowFilter= myColumn LIKE '%"+MyTextBox.Text+"%'";


The problem is when user enters string which contains % or * I get an
exception:
System.Data.EvaluateException: Error in Like operator: the string
pattern ' ' is invalid.


DataTable can contain strings with those characters and users must be
able to define filter with them.


Tomasz
 
W

William Ryan eMVP

Change this: LIKE '%"+MyTextBox.Text+"%'";
To THis: LIKE "%'" + MyTextBox.Text + "'%";

It Should work
HTH,

Bill
 
M

marcin

William said:
Change this: LIKE '%"+MyTextBox.Text+"%'";
To THis: LIKE "%'" + MyTextBox.Text + "'%";

It Should work
HTH,
this time I got exception:
missing operand before Mod operator,
no matter what user enters in MyTextBox

Tomasz
 
N

Nicholas Paldino [.NET/C# MVP]

Tomasz,

You should prefix the special characters with the \ character, to
indicate that it should be escaped. You will have to do a search and
replace in the string that is passed to you. For a list of characters that
need to be escaped, check out the documentation for the Expression property
on the DataColumn class.

Hope this helps.
 
M

marcin

Nicholas said:
Tomasz,

You should prefix the special characters with the \ character, to
indicate that it should be escaped. You will have to do a search and
replace in the string that is passed to you. For a list of characters that
need to be escaped, check out the documentation for the Expression property
on the DataColumn class.

Hope this helps.


I got exception that string pattern is invalid.

I am afraid that \ character is a escape character for common string,
we need some escape characters for sql statement

Tomasz
 
M

Miha Markic [MVP C#]

Hi marcin,

According to help you should escape both % and * into square brackets: % ->
[%] and * -> [*].

help:
"Both the * and % can be used interchangeably for wildcards in a LIKE
comparison. If the string in a LIKE clause contains a * or %, those
characters should be escaped in brackets ([]). If a bracket is in the
clause, the bracket characters should be escaped in brackets (for example
[[] or []]). "
 
M

marcin

Miha said:
Hi marcin,

According to help you should escape both % and * into square brackets: % ->
[%] and * -> [*].

help:
"Both the * and % can be used interchangeably for wildcards in a LIKE
comparison. If the string in a LIKE clause contains a * or %, those
characters should be escaped in brackets ([]). If a bracket is in the
clause, the bracket characters should be escaped in brackets (for example
[[] or []]). "


thanks a lot
it works

Tomasz
 
Joined
Sep 9, 2011
Messages
1
Reaction score
0
Miha Markic [MVP C#] wrote:

> Hi marcin,
>
> According to help you should escape both % and * into square brackets: % ->
> [%] and * -> [*].
>
> help:
> "Both the * and % can be used interchangeably for wildcards in a LIKE
> comparison. If the string in a LIKE clause contains a * or %, those
> characters should be escaped in brackets ([]). If a bracket is in the
> clause, the bracket characters should be escaped in brackets (for example
> [[] or []]). "
>



thanks a lot
it works

Tomasz

as is your code ...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top