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
- [DataType(DataType.DateTime)]
- [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true, NullDisplayText = "NULL")]
- public Nullable
Tanggal_Lahir { get; set; }
this is the PasienController.cs
- [HttpGet]
- public IActionResult TambahData()
- {
- return View();
- }
- public IActionResult TambahData(PasienModel pasienModel)
- {
- if (ModelState.IsValid)
- {
- using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_klinik")))
- {
- 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))
- {
- con.Open();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.ExecuteNonQuery();
- return RedirectToAction("Index", "Dokter");
- }
- }
- }
- else
- {
- return View(pasienModel);
- }
- }
this is the View TambahData.cshtml
- class="form-group row">
- ="Tanggal_Lahir" class="col-sm-2 col-form-label">Tanggal Lahir
class="col-sm-10">- @Html.TextBoxFor(model => model.Tanggal_Lahir, new { @class = "form-control datepicker", placeholder = "Enter Date", id = "Date", autocomplete = "off" })
- for="Tanggal_Lahir" class="text-danger">
this is the Javascript to show calender
- "~/JqueryCalender/jquery-ui-1.12.1.custom/jquery-ui.min.css" rel="stylesheet" />


Amit GuptaRank #190 · 12.8K reputationPosted Apr 11, 2021, 9:45 AM EDT
Amit GuptaRank #190 · 12.8K reputationPosted Apr 11, 2021, 1:30 PM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Apr 11, 2021, 2:30 AM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Apr 11, 2021, 4:09 AM EDT
Amit GuptaRank #190 · 12.8K reputationPosted Apr 11, 2021, 4:29 AM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Apr 11, 2021, 4:52 AM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Apr 11, 2021, 12:07 PM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Apr 12, 2021, 12:21 AM EDT