DataView + RowFilter +Like %

  • Thread starter Thread starter marcin
  • Start date Start date
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
 
Change this: LIKE '%"+MyTextBox.Text+"%'";
To THis: LIKE "%'" + MyTextBox.Text + "'%";

It Should work
HTH,

Bill
 
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
 
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.
 
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
 
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 []]). "
 
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
 
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 ...?
 
Back
Top