-1

I am having problems with a DataView.RowFilter not returning the proper row that it should return. I currently keep Col2 has my variable name in the database and Col3 is the value associated with the variable name. When I try pulling the dataview with the filter below it doesn't add a row. When I remove the filter all my records are there. Anyone know why or can explain to me what is wrong with my filter or can a dataview not return only one row?

.RowFilter = String.Format("[Col2] LIKE '%{0}%' AND [Col3] = '{1}'", Name, Value)

An example of a search would be Col2 I am searching for First Name and Col3 would be I am searching for the Name Joe.

  • It is not clear what you are trying to do here. The syntax for the expression is COLNAME = VALUE. Why do you have square brackets around values? Can you add an example of the values you are searching for with this syntax? – Steve Aug 17 '16 at 13:54
  • I read in a different article the [] is suppose to remove extra spaces – DotNet Programmer Aug 17 '16 at 13:55
  • I think that the square brackets in the value part of your expression are takes as literals and added to the string you are searching for – Steve Aug 17 '16 at 13:57
  • If you are searching for a firstname with Joe as value then you write _"[FirstName] LIKE '%Joe%'"_ Of course in case of exact match you remove the wild cards and in case of exact case match you remove the LIKE and use = – Steve Aug 17 '16 at 13:59
  • I just removed the [] and it went past the .RowFilter without breaking. It still won't populate any data – DotNet Programmer Aug 17 '16 at 14:01
  • Could you [edit] your current question adding the revised RowFilter expression? – Steve Aug 17 '16 at 14:03
  • It has been revised – DotNet Programmer Aug 17 '16 at 14:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121175/discussion-between-steve-and-dotnet-programmer). – Steve Aug 17 '16 at 14:09
  • 1
    Just in case someone finds this question, I want to point out that use of square brackets around the field name is valid; personally, I prefer their use as it sanitizes the field name if the field name contains any characters considered special by the expression parser. The `RowFilter` property follows the syntax rules for the [DataColumn.Expression Property](https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=vs.110%29.aspx). – TnTinMn Aug 17 '16 at 14:43

1 Answers1

0

It turns out it wasn't the filter, when I was stepping through other parts of the code it broke when checking for permissions if the user had the right to edit the database.

  • Your code will likely break if the user searches for a name like O'Brien (unless the `Name` variable is already sanitised) – SSS Aug 18 '16 at 01:54