London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Clearing a date field with code - You must enter a value between 01/01/1753 and 31/12/9999

Vote:
 

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.

//Get the content reference of the page to update.
                ContentReference contentReference = new ContentReference(model.PropertyID);
                //Get the writeable version of the content reference.
                HeritagePropertyLandingPage propertyPage = _contentRepository.Get(contentReference).CreateWritableClone() as HeritagePropertyLandingPage;
                //Make the changes.
                propertyPage.EmergencyText.StartDateTime = DateTime.MinValue;

                //Save.
                _contentRepository.Save(propertyPage, EPiServer.DataAccess.SaveAction.Publish);

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.

#189979
Mar 28, 2018 16:09
Vote:
 

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.

#190005
Mar 29, 2018 8:25
Vote:
 

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.

#190029
Mar 29, 2018 14:03
Vote:
 

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.

#190031
Mar 29, 2018 14:10
Vote:
 

What does your definition of HeritagePropertyLandingPage look like?

#190033
Mar 29, 2018 14:26
Vote:
 
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; }
    }
#190039
Mar 29, 2018 15:07
Vote:
 

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.

#190042
Mar 29, 2018 15:31
Vote:
 

We do use  pubish and expiration for most dates, this is a special situation though.  Your answer worked perfect.  Cheers.

#190128
Apr 03, 2018 10:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.