I have a DataTable with some data and I want to filter with LIKE query so I have written a code like below and everything working fine, I'm generating the query and passing it to RowFilter object of DataView.
if (results.Count() == 2)
{
if (results[0].ToString() == "" && results[1].ToString() != "")
{
searchString = String.Format("[" + searchBy + "] LIKE '%{0}'", results[1].ToString());
}
else if (results[0].ToString() != "" && results[1].ToString() == "")
{
searchString = String.Format("[" + searchBy + "] LIKE '{0}%'", results[0].ToString());
}
else if (results[0].ToString() != "" && results[1].ToString() != "")
{
searchString = String.Format("[" + searchBy + "] LIKE '{0}%{1}'", results[0].ToString(), results[1].ToString());
}
}
if (searchString != "")
{
DataView dv = new DataView(AccountList.Tables[0]);
dv.RowFilter = searchString;
var tab = dv.ToTable();
DataSet data_set = new DataSet();
data_set.Tables.Add(tab);
ProcessDataSetAndLoadData(data_set);
}
In above code segment is working fine except last else if condition, In that condition I'm getting the searchString like this,
[AccountCode] LIKE 'c%l'
When I passing this to RowFilter I'm getting the eror like below,
Error in Like operator: the string pattern 'c%l' is invalid.
From all other conditions I'm getting the searchString like below and those conditions are working fine.
[AccountCode] LIKE '%c%'
[AccountCode] LIKE 'c%'
[AccountCode] LIKE '%c
My Question is why I'm getting the error only for the string like this ?. [AccountCode] LIKE 'c%l'
Please Advise me to complete my task.