First of all I like to state that I am new to c# ASP.NET. I am using ASP.NET MVC 5 and Entity framework with a code first approach.
When I try to insert a new record into the database I get this error
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
Here is what I did so far.
First I created the following model
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ScripterEngine.Models
{
[Table("campaign_calllogs")]
public class Calllog
{
[Key]
public int id { get; set; }
public int campaign_id { get; set; }
public int callid { get; set; }
public string dialer_campaign_id { get; set; }
public string dialing_mode { get; set; }
public int attempts { get; set; }
public string phone { get; set; }
public int i3_identity { get; set; }
public string wrapupcode { get; set; }
public string call_notes { get; set; }
public string completed_by { get; set; }
public string callidkey { get; set; }
public string direction { get; set; }
public string local_id { get; set; }
public string local_location { get; set; }
public string local_name { get; set; }
public string remote_id { get; set; }
public string remote_location { get; set; }
public string remote_name { get; set; }
public DateTime? connected_at { get; set; }
public DateTime? disconnected_at { get; set; }
public DateTime? call_placed_at { get; set; }
public DateTime? assigned_at { get; set; }
public DateTime? completed_at { get; set; }
public DateTime? creation_time { get; set; }
public DateTime created_at { get; set; }
public DateTime? updatd_at { get; set; }
}
}
Then I am trying to add a new record from a controller.
Here is what I have done in my add action in the controller
public string Add()
{
dynamic respond = new ExpandoObject();
respond.logged = false;
int campaignId = Parser.intVal(Request["campaign_id"]);
int callid = Parser.intVal(Request["callid"]);
int i3Identity = Parser.intVal(Request["i3_identity"]);
int attempts = Parser.intVal(Request["attempts"]);
DateTime? assigneAt = Parser.dateTimeVal(Request["assigned_at"]);
DateTime? completedAt = Parser.dateTimeVal(Request["completed_at"]);
DateTime? creationTime = Parser.dateTimeVal(Request["creation_time"]);
DateTime? connectedAt = Parser.dateTimeVal(Request["connected_at"]);
DateTime? disconnectedAt = Parser.dateTimeVal(Request["disconnected_at"]);
DateTime? callPlacedAt = Parser.dateTimeVal(Request["call_placed_at"]);
if (campaignId > 0 && callid > 0 && i3Identity > 0 && ModelState.IsValid)
{
Calllog log = new Calllog();
log.campaign_id = campaignId;
log.callid = callid;
log.i3_identity = i3Identity;
log.attempts = attempts;
log.assigned_at = assigneAt;
log.completed_at = completedAt;
log.creation_time = creationTime;
log.connected_at = connectedAt;
log.disconnected_at = disconnectedAt;
log.call_placed_at = callPlacedAt;
log.phone = Request["phone"];
log.wrapupcode = Request["wrapupcode"];
log.callidkey = Request["callidkey"];
log.direction = Request["direction"];
log.completed_by = Request["completed_by"];
log.dialer_campaign_id = Request["dialer_campaign_id"];
log.call_notes = Request["call_notes"];
log.completed_by = Request["completed_by"];
log.dialing_mode = Request["dialing_mode"];
log.local_id = Request["local_id"];
log.local_location = Request["loacl_location"];
log.local_name = Request["local_name"];
log.remote_id = Request["remote_id"];
log.remote_location = Request["remote_location"];
log.remote_name = Request["remote_name"];
db.Calllog.Add(log);
db.SaveChanges();
respond.logged = true;
}
return JsonHelpers.ToJson(respond);
}
The exception points at this line db.SaveChanges();
First of all what is the different between datetime
and datetime2
? is one nullable
and the other not?
What could be causing this error?