Per Rocky's advice, my business objects have public string properties for EffDate and ObsDate (EffectiveDate and ObsoleteDate) although the private variables are SmartDates.
I'm trying to filter on these values using ObjectListView and having a bit of trouble.
My Filter expression is:
EffDate >= #1/1/2002# AND EffDate <= #12/31/2004#
I get this error message:
Cannot perform '>=' operation on System.String and System.DateTime.
How are other folks handling this? I'm looking for best practice here. :)
Now I'm having a problem when the date is null (which means the string representation = String.Empty).
As soon as the filter runs into an empty date it barfs with this message:
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: String was not recognized as a valid DateTime.
I'll just mention that the programmer responsible for that message in DataView should have supplied the offending value as part of the error message... :(
I've tried testing for null, but it doesn't seem to short-circuit the exception processing to do so. :(
Here's my expression:
IIF(ISNULL(OBSDATE,'Y') = 'Y',#01/01/1901#,CONVERT(ObsDate,System.DateTime)) >= #"+ filterObsDtSt + "#"
filterObsDtSt has a value of "1/1/1901"
ObsDate has a value of "".
I don't want to be the guy to go against Rocky's advice - especially with his own framework! - but why are you working with your dates as strings? Why not just use the DateTime value? Then your empty date will either be DateTime.MinValue or DateTime.MaxValue, which you should be able to work with without all the string futzing.
The old IIF function evaluates both parts! It will always crash. It does not do what you think it does.
I believe a new IIF function was added in VB9 that should do what you want. You are better off writing the 3 lines of code to do it the right way anyway.
You can expose your properties as Dates too. Sometimes you want to display the string for binding but sort on the real underlying date.
There are several reasons I'm exposing my date properties as strings instead of SmartDates or DateTimes.
Reasons against using DateTime:
Reasons against SmartDate:
Reasons against String:
So far, to me, it's seemed like a Monty Hall gameshow where all three doors had glasses of hemlock as the prize. :(
I finally got it to work. Had to turn the IIF and CONVERT functions inside out. Here's the first part of the test:
CONVERT(IIF(LEN(ObsDate) = 0,'01/01/1901',ObsDate),'System.DateTime') >= #"+ filterObsDtSt + "#"
I solved the sorting problem some time ago, thanks to an earlier post of yours. :)
Copyright (c) Marimer LLC