Tri Setia

Tri Setia

  • 1.3k
  • 464
  • 24.4k

How to save DateTime format in ASP.NET MVC .NET CORE

Apr 11 2021 5:50 AM
Hi guys,, I'm try to save data in sql server with DateTime format, I'm get error like this 
 
SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated.
 
in the Controller I have been try to + Convert.ToDateTime(pasienModel.Tanggal_Lahir) + but not working. Any help could be appreciated.
 
 
this is the PasienModel.cs
  1. [DataType(DataType.DateTime)]    
  2. [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true, NullDisplayText = "NULL")]    
  3. public Nullable<System.DateTime> Tanggal_Lahir { getset; }  
this is the PasienController.cs
  1. [HttpGet]    
  2. public IActionResult TambahData()    
  3. {    
  4.     return View();    
  5. }    
  6. public IActionResult TambahData(PasienModel pasienModel)    
  7. {    
  8.     if (ModelState.IsValid)    
  9.     {    
  10.         using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_klinik")))    
  11.         {    
  12.             using (SqlCommand cmd = new SqlCommand("Insert Into Pasien (Nama_Pasien,Alamat,Jenis_Kelamin,Tanggal_Lahir,Status) Values ('" + pasienModel.Nama_Pasien + "','" + pasienModel.Alamat + "','" + pasienModel.Jenis_Kelamin + "','" + Convert.ToDateTime(pasienModel.Tanggal_Lahir) + "','" + pasienModel.Status + "')", con))    
  13.             {    
  14.                 con.Open();    
  15.                 cmd.Connection = con;    
  16.                 cmd.CommandType = CommandType.Text;    
  17.                 cmd.ExecuteNonQuery();    
  18.                 return RedirectToAction("Index""Dokter");    
  19.   
  20.             }    
  21.         }    
  22.     }    
  23.     else    
  24.     {    
  25.         return View(pasienModel);    
  26.     }    
  27. }  
this is the View TambahData.cshtml
  1. <div class="form-group row">    
  2.      <label asp-for="Tanggal_Lahir" class="col-sm-2 col-form-label">Tanggal Lahir</label>    
  3.          <div class="col-sm-10">    
  4.                @Html.TextBoxFor(model => model.Tanggal_Lahir, new { @class = "form-control datepicker", placeholder = "Enter Date", id = "Date", autocomplete = "off" })    
  5.               <span asp-validation-for="Tanggal_Lahir" class="text-danger"></span>    
  6.          </div>    
  7. </div>   
this is the Javascript to show calender
  1. <script src="~/plugins/jquery/jquery.min.js"></script>    
  2.    <link href="~/JqueryCalender/jquery-ui-1.12.1.custom/jquery-ui.min.css" rel="stylesheet" />    
  3.    <script src="~/plugins/moment/moment.min.js"></script>    
  4.    <script type="text/javascript">    
  5.        $(document).ready(function () {    
  6.            $('.datepicker').datepicker({    
  7.                dateFormat: "yy/mm/dd"    
  8.            });    
  9.        });    
  10. </script>

Answers (8)