Question

I hope someone can help, I've been struggling with this for a few days now.

I am trying to filter out some records using a rowfilter that has a datetime in one of the columns that is older than 15 minutes ago, looking at it, this looks to me as though it would do it:

    Dim TimeStr As String
    TimeStr = String.Format(CultureInfo.InvariantCulture, "#{0}#", Now.AddMinutes(-15))
    AlertsView.RowFilter = "StatusId < 3 AND LastEmailed < " & TimeStr

Before the filter there are 6 rows in the view but after the filter there are none, i know the StatusId field is correct as if i filter on just that it works fine. All 6 rows have a LastEmailed value of 2013-09-03 23:06:44.813

I'm thinking it might be something with the string conversion but i am stuck.

Many thanks

Was it helpful?

Solution

Anything between the two # characters in a RowFilter expression will be ultimately passed into DateTime.Parse(s, CultureInfo.InvariantCulture).

This should be mentioned in the MSDN documentation for DataColumn.Expression, but it isn't. However, if you dig really deep you can find it in the constructor of the internal class System.Data.ConstNode.

So as long as you are generating values that are unambiguous in the invariant culture, you should be fine. Just to be on the safe side, you might want to use the "s" format specifier to give you a value like 2013-12-31T12:34:56. Change your format string to "#{0:s}#".

Or much cleaner:

Dim TimeStr As String
TimeStr = Now.AddMinutes(-15).ToString("s", CultureInfo.InvariantCulture)
AlertsView.RowFilter = "StatusId < 3 AND LastEmailed < #" & TimeStr & "#"

String.Format doesn't buy you much here.

Also - just out of curiosity, are you sure you are not comparing apples to oranges? It wouldn't be uncommon for a database to store UTC time in a field like LastEmailed. You might need to use DateTime.UtcNow instead of just Now (which is an alias for DateTime.Now).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top