November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
An option is to have your property declared as a nullable DateTime (DateTime?) instead, then you can assign the value null to it to clear it.
DateTime.MinValue is 00:00:00.0000000 UTC, January 1, 0001 which is fine to use in C#.
However SQL server doesn't support dates earlier than January 1, 1753, therefore they can't be saved.
As Johan said using DateTime? is the recommended way to handle it.
Thanks for the replies. I just find it strange via the editor you can enter a blank date and it saves it ok. So they must have logic there that sets a DateTime.MinValue to a null sql date. That's how I've done this kind of thing before.
I'm not sure I can risk changing the field strcuture now we have effectively live data. If I change it do nullable I'm not sure the impact on all the existing records.
It has this property: [Display( Name = "Emergency", GroupName = ContentGroupNames.Alerts, Order = 10)] public virtual EmergencyTextBlock EmergencyText { get; set; } Which itself contains: public class EmergencyTextBlock : BlockData { [Display( Name = "Emergency text", GroupName = ContentGroupNames.Content)] [UIHint(UIHint.Textarea)] public virtual string EmergencyText { get; set; } [Display( Name = "Display from", GroupName = ContentGroupNames.Content)] public virtual DateTime StartDateTime { get; set; } [Display( Name = "Display to", GroupName = ContentGroupNames.Content)] public virtual DateTime EndDateTime { get; set; } }
A shot in the dark but can you test these two approaches:
propertyPage["StartDateTime"]=null; propertyPage.EmergencyText["StartDateTime"]=null;
For future reference i would like to recommend utilizing the built-in support for scheduled publish and expiration, if a block has expired there is no need to load it from the database before checking dates.
We do use pubish and expiration for most dates, this is a special situation though. Your answer worked perfect. Cheers.
When I try and set a DateTime field to DateTime.MinValue and save in code it gives me this error: You must enter a value between 01/01/1753 and 31/12/9999.
Works fine if I set it to a real date value, but I have the need to be able to clear this field out.
If I clear out the date field via the cms editor it allows me to do this. When I then retrieve the value in code it is a DateTime.MinValue equivalent.
Any ideas? I guess I could change the SaveAction to not validate, but then I don't see why Episerver itself via the editor lets me save a blank date, but I can't via code.