I am using ASP.NET MVC and I am trying to save values from my view in a SQL Server table using stored procedures. The problem that the procedure doesn't work because of an issue with the Date
attribute. I don't get any error or exception but when I debug, the debugger goes direct to catch after opening the SQL Server connection and I can catch this exception :
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
This is what I wrote in my controller :
[HttpPost]
public ActionResult Save_Bestellung(Bestellung bs)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
try
{
if (ModelState.IsValid)
{
BestellungManagement bdb = new BestellungManagement();
if (bdb.AddBestellung(bs))
{
return View("~/Views/Bestellung/Index.cshtml");
ViewBag.Message = "Bestellung saved Successfully";
ModelState.Clear();
}
}
return RedirectToAction("Index");
}
catch
{
return JavaScript("alert('Order is Wrong ! Please verify your data !!')");
}
}
and this is my class BestellungManagement
:
public bool AddBestellung(Bestellung bs)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DeliveryCon"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("AddNewBestellung", con))
{
cmd.Parameters.AddWithValue("@Date", bs.Date);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
return true;
else
return false;
}
}
}
catch(Exception ex)
{
string e = ex.Message;
return false;
}
}
In model Bestellung.cs
I have:
public DateTime Date { get; set; }
Database Schema :
[Date] DATETIME NOT NULL,
In the view :
<label>DATE : </label>
<input id="datepicker" name="Text" type="text" value="mm/dd/yyyy" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'mm/dd/yyyy';}" required="">
the datepicker Function :
<script>
$(function()
{
$("#datepicker").datepicker({ minDate: 0 });
});
</script>