• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Problem in dataView.RowFilter

I work on vs 2005 c#
Have dataview and I made  rowfilte

string Myaddress="alexandria";
Just like dataView1.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";
And its work fine

Problem when I when to make operator or for more than one letter
string Myaddress="al[ea]xandria";
Just like dataView.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";

Error >> Error in Like operator: the string pattern '%[ea]%' is invalid.

I want to make operator or in letters like sql server .
Whats is the slove ?????
0
alnahas1
Asked:
alnahas1
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
since the Myaddress contans brackets u need to modify the filter a little bit.
chcek http://www.csharp-examples.net/dataview-rowfilter/
the first section explans how to use special charachters in filter.
0
 
alnahas1Author Commented:
This is a great website but i afraid it's not help me
please any ather solve
thanks in advance
0
 
Dhanasekaran SengodanCommented:
From
http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression(vs.71).aspx

 (last sentence is important):

WILDCARD CHARACTERS

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 []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example:

"ItemName LIKE '*product*'"

"ItemName LIKE '*product'"

"ItemName LIKE 'product*'"

Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
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.

 
Meir RivkinFull stack Software EngineerCommented:
try:

dataView.RowFilter = "Columnaddress like '%" + EscapeLikeValue(Myaddress) + "%' ";

public static string EscapeLikeValue(string valueWithoutWildcards)
{
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < valueWithoutWildcards.Length; i++)
  {
    char c = valueWithoutWildcards[i];
    if (c == '*' || c == '%' || c == '[' || c == ']')
      sb.Append("[").Append(c).Append("]");
    else if (c == '\'')
      sb.Append("''");
    else
      sb.Append(c);
  }
  return sb.ToString();
}

0
 
alnahas1Author Commented:
Mr sedgwick:

this slove no appear error and also make slove to my problem
i mean al[ae]xandria  is convert to alxandria
i want dataview filter search for alexandria   and  alaxandria
0
 
bhagwantsinghCommented:
If a pattern in a LIKE clause contains any of these special characters * % [ ], those characters must be escaped in brackets [ ] like this [*], [%], [[] or []].


string Myaddress="al[[]ea[]]xandria";

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
@alnahas1

if i'm not mistaken ur misusing the LIKE clause.
i assumed that the brackets are part of the string ur trying to filter.
however, ur logic is 2 use the letters in the brackets to produce multiple options based on the number of the characters inside the brackets.

so what u mean basically is that if your Myaddress string is: "123[XYZ]123"

your filter suppose to be look like this:
dataView.RowFilter = "Columnaddress like '%123X123%'  or
Columnaddress like '%123Y123%'  or
Columnaddress like '%123Z123%' ";

if this what u mean u need to build the expression yourself cause this kind of logic is not supported.

what i'd do is to write a function which accept column name and expression (i.e. "al[ea]xandria") and return the filter expression with the logic u want to apply.

check the below code:

static void Main(string[] args)
        {
            string exp = GenerateOptionsFilter("City", "New[ -]York");
            //City='New York' or City='New-York' 
        } 
        static string GenerateOptionsFilter(string column, string input)
        {
            string[] tokens = input.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Count() != 3)
            {
                //invlid input
            }
            StringBuilder sb = new StringBuilder();
            foreach (char c in tokens[1])
            {
                sb.AppendFormat("{0}='{1}", column, tokens[0]);
                sb.Append(c);
                sb.Append(tokens[2]);
                sb.Append("' or " );
            } 
            string exp = sb.ToString();
            return exp.Substring(0, exp.Length - 3);
        }

Open in new window

0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now