Hello,
I want to save an attribute Date with type DateTime in my database but I got always this exception :
ex.Message = "Procedure or function 'AddNewBestellung' expects parameter '@Date', which was not supplied."
The 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 !!')");
}
}
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", SqlDbType.DateTime2).Value = 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 :
public DateTime Date { get; set; }
In 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>
What I have tried:
I tried to change the type of Date from DateTime to datetime2 but the problem is not resolved. I think that the issue is with the JS Function because when I debug, the parameter Date got always Null Value for the time like this :
Date = {10.02.2017 00:00:00} when I set Today's date.