Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.
Posted
Comments
F-ES Sitecore 2-Oct-17 9:53am    
First thing is to change your code to use AddWithValue properly, the second param is the value, not the type.

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx
Anouar2002 5-Oct-17 3:32am    
In my code, the Second parameter is the value "SqlDbType.DateTime2).Value", anyway I don't think that the problem is here because it didn't work when I write like this :
cmd.Parameters.AddWithValue("@Date", bs.Date);
Richard Deeming 3-Oct-17 13:36pm    
If the property is called Date, why have you set the name of your <input> to "Text"?

The name of the input needs to match the name of the property if you want model binding to work.
Anouar2002 5-Oct-17 3:24am    
oh sorry, I have copied an old version from my View, the input is named "Date" which is the name of the model property.
And when I put Date as nullable DateTime parameter, the modelState will get the value "false" and the error is "The value '10/26/2017' is not valid for Date."
Richard Deeming 5-Oct-17 9:29am    
That sounds like an issue with the culture settings. It's submitting a US-format date, but it sounds like your server is expecting a different format.

If possible, try submitting the value using the yyyy/MM/dd format.

Alternatively, you could use the native date input[^], which is rapidly gaining browser support[^]. (IE11 and desktop Safari don't support it; the current version of Firefox has support behind a flag, and the next version has global support.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900