SqlDateTime overflow error

Topics: Administration, General
Jan 2, 2014 at 7:08 AM
Edited Jan 10, 2014 at 7:56 AM
I am unable to figure out why i am getting this error even i am selecting the proper date values. I also want when user click to create course, dates should be filled automatically.

"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM"

CoursePart.cs

using System;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Records;
using Orchard.ContentManagement.Utilities;

namespace Orchard.Club.Models
{
    public class CoursePart : ContentPart<CoursePartRecord>
    {
        public string Name
        {
            get { return Record.Name; }
            set { Record.Name = value; }
        }

        public string Description
        {
            get { return Record.Description; }
            set { Record.Description = value; }
        }

        public string Location
        {
            get { return Record.Location; }
            set { Record.Location = value; }
        }

        public string Category
        {
            get { return Record.Category; }
            set { Record.Category = value; }
        }

        [DataType(DataType.Date)]
        public DateTime? StartDate
        {
            get { return Record.StartDate; }
            set { Record.StartDate = value; }
        }

        [DataType(DataType.Date)]
        public DateTime? EndDate
        {
            get { return Record.EndDate; }
            set { Record.EndDate = value; }
        }

        public double Fees
        {
            get { return Record.Fees; }
            set { Record.Fees = value; }
        }
    }

    public class CoursePartRecord : ContentPartRecord
    {
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual string Location { get; set; }
        public virtual string Category { get; set; }
        public virtual DateTime? StartDate { get; set; }
        public virtual DateTime? EndDate { get; set; }
        public virtual double Fees { get; set; }
    }
}

Course.cshtml

@model Orchard.Club.Models.CoursePart

@using Telerik.Web.Mvc.UI;   
     
@{
    Script.Include("~/Scripts/jquery-1.6.2.min.js");
    Script.Include("~/Scripts/modernizr-2.0.6-development-only.js");
}      

<fieldset>     
    <div class="editor-label">@Html.LabelFor(x => x.Name)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </div>
    
    <div class="editor-label">@Html.LabelFor(x => x.Description)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Description)
        @Html.ValidationMessageFor(x => x.Description)
    </div>
    
    <div class="editor-label">@Html.LabelFor(x => x.Location)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Location)
        @Html.ValidationMessageFor(x => x.Location)
    </div>

    
    <div class="editor-label">@Html.LabelFor(x => x.Category)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Category)
        @Html.ValidationMessageFor(x => x.Category)
    </div>

    <div class="editor-label">@Html.LabelFor(x => x.StartDate)</div>
    <div class="editor-field">
        @*@(Html.Telerik().DatePickerFor(m => m.StartDate))*@
        @Html.EditorFor(x => x.StartDate)
    </div>
        
    <div class="editor-label">@Html.LabelFor(x => x.EndDate)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.EndDate)
        @*@(Html.Telerik().DatePickerFor(m => m.EndDate))*@
    </div>

    
    <div class="editor-label">@Html.LabelFor(x => x.Fees)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Fees)
        @Html.ValidationMessageFor(x => x.Fees)
    </div>  
</fieldset>

@( Html.Telerik().ScriptRegistrar().Globalization(true))

@( Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group
                    .Add("telerik.common.min.css")
                    .Add("telerik.metro.min.css"))
)

Create.cshtml

@{ Layout.Title = T("Create User").ToString(); }

@using (Html.BeginFormAntiForgeryPost()) { 
    @Html.ValidationSummary() 
    // Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
    @Display(Model)    
 } 

Controller Snippet

        public ActionResult Create()
        {                     
            var course = _orchardService.ContentManager.New<CoursePart>("Course");
            dynamic model = _orchardService.ContentManager.BuildEditor(course);            
            return View((object)model);                                         
        }

        [HttpPost, ActionName("Create")]
        public ActionResult CreatePOST()
        {            
            var contentItem = _contentManager.New("Course");
            _contentManager.Create(contentItem, VersionOptions.Draft);

            dynamic model = _contentManager.UpdateEditor(contentItem, this);

            _contentManager.Publish(contentItem);
            _orchardService.Notifier.Information(new LocalizedString("Course has been created."));                                        

            return RedirectToAction("Index");
        }

CoursePartHandler

using System;
using Orchard.Club.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Orchard.Services;

namespace Orchard.Club.Handlers
{
    public class CoursePartHandler : ContentHandler
    {
        public CoursePartHandler(IRepository<CoursePartRecord> repository)
            {
                Filters.Add(StorageFilter.For(repository));
                Filters.Add(new ActivatingFilter<CoursePart>("Course"));
                OnCreating<CoursePart>(Creating);
            }

            private void Creating(CreateContentContext context, CoursePart part)
            {
                part.StartDate = DateTime.Now.Date;
                part.EndDate = DateTime.Now.Date;
            }
       
    }
}
Developer
Jan 2, 2014 at 10:20 AM
Try changing your DateTime properties on your Record classes to nullable DateTimes (DateTime?).
One way to set an initial DateTime value is to do so right after your call to _contentManager.New<CoursePart>("Course").
Another way is to do so from a content handler by setting a method via the OnInitialized, e.g:
// Content handler constructor
public CoursePartHandler(IClock clock){
   OnInitialized<CoursePart>((context, part) => part.StartDate = clock.UtcNow);
}
Marked as answer by mrhammad on 1/10/2014 at 2:42 AM
Developer
Jan 4, 2014 at 9:53 PM
Apart from the DateTime being nullable as Sipke menioned there are a few other gotchas for records as well, see this sample: https://orchardtrainingdemo.codeplex.com/SourceControl/latest#Models/PersonRecord.cs
Marked as answer by mrhammad on 1/10/2014 at 2:42 AM
Jan 10, 2014 at 7:46 AM
I tried changing the date to nullable but nothing happens same error i am getting when i create the course. Also i cant see any special gotchas spike did for dates in
https://orchardtrainingdemo.codeplex.com/SourceControl/latest#Models/PersonRecord.cs

Also he didnt even save the person record to database. Maybe i am missing something in it ?

I cant find any single source which shows saving date to database using orchard module. I stuck at this point.
Jan 10, 2014 at 7:57 AM
Edited Jan 10, 2014 at 7:59 AM
Ok sorry i forgot to make datetime nullable in CoursePartRecord. Now i am getting another error

could not execute batch command.[SQL: SQL not available]

then another error in Create Content:

null id in Orchard.ContentManagement.Records.ContentTypeRecord entry (don't flush the Session after an exception occurs)
Jan 10, 2014 at 9:41 AM
Resolved thanks guys for the help.