Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>  

@( 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 Course = _contentManager.New("Course");            
        var userPart = Course.As<CoursePart>();            
        userPart.StartDate = _clock.UtcNow;
        userPart.EndDate = _clock.UtcNow;
        // Store the new user into the database
        _orchardService.ContentManager.Create(Course);

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

        _contentManager.Publish(Course);

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

        return RedirectToAction("Index");
    }
 ```

PartDriver.cs

  namespace Orchard.Club.Drivers
  {
  public class CourserPartDriver : ContentPartDriver<CoursePart> {

    protected override DriverResult Display(
        CoursePart part, string displayType, dynamic shapeHelper)
    {
        return ContentShape("Parts_Course",
            () => shapeHelper.Parts_Course(
                Name: part.Name,
                Description: part.Description));
    }

    //GET
    protected override DriverResult Editor(CoursePart part, dynamic shapeHelper)
    {
        return ContentShape("Parts_Course_Edit",
            () => shapeHelper.EditorTemplate(
                TemplateName: "Parts/Course",
                Model: part,
                Prefix: Prefix));
    }

    //POST
    protected override DriverResult Editor(CoursePart part, IUpdateModel updater, dynamic shapeHelper)
    {
        updater.TryUpdateModel(part, Prefix, null, null);
        return Editor(part, shapeHelper);
      }
    }
   }

CoursePartHandler.cs

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

            OnInitializing<CoursePart>(AssignDates);

            OnLoaded<CoursePart>(SetDates);
        }

        private void SetDates(LoadContentContext context, CoursePart part)
        {
            var obj = context.ContentItem.As<CoursePart>();
            obj.StartDate = DateTime.Now;
            obj.EndDate = DateTime.Now;

            part.StartDate = DateTime.Now;
            part.EndDate = DateTime.Now;
        }

        protected void AssignDates(InitializingContentContext context, CoursePart part)
        {
            var obj = context.ContentItem.As<CoursePart>();
            obj.StartDate = DateTime.Now;
            obj.EndDate = DateTime.Now;

            part.StartDate = DateTime.Now;
            part.EndDate = DateTime.Now;
        }
      }
     }
     }

Course.cshtml

@model Orchard.Club.Models.CoursePart

<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.EditorFor(x => x.StartDate)
    @Html.ValidationMessageFor(x => x.StartDate)
</div>

<div class="editor-label">@Html.LabelFor(x => x.EndDate)</div>
<div class="editor-field">
    @Html.EditorFor(x => x.EndDate)    
    @Html.ValidationMessageFor(x => x.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>
share|improve this question
 
What about setting breakpoints? –  Elliot Tereschuk Jan 2 at 8:31
add comment

1 Answer

You have:

    var contentItem = _contentManager.New("Course");
    _contentManager.Create(contentItem, VersionOptions.Draft);
    dynamic model = _contentManager.UpdateEditor(contentItem, this);
    _contentManager.Publish(contentItem);

The .Create() call generates INSERT statements for contentItem. Then, additional changes made to the item will generate UPDATE statements.

At the point in time .Create() is called, you still have zeros in your DateTime fields of your object. This is why SQL Server Compact is unable to create your item. You need to fill in default values before this point.

share|improve this answer
 
Then how can i resolve this issue? –  Hammad Bukhari 2 days ago
 
Please see my original post, i have changed the code. I am getting time correctly to StartDate and EndDate variables but still it giving me error. –  Hammad Bukhari yesterday
 
Are you able to post your Handler and Driver code? –  Katsuyuki Ohmuro yesterday
 
Yes already done. please see my original post. –  Hammad Bukhari yesterday
 
Ok if this problem cant be solved, i can change the model to make StartDate as string to store in database but problem here is how can i display in View to select date?e.g. datepicker if field is string –  Hammad Bukhari yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.