TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString =
"{0:yyyy-MM-dd}"
, ApplyFormatInEditMode =
true
, NullDisplayText =
"NULL"
)]
public
Nullable<System.DateTime> 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
<div
class
=
"form-group row"
>
<label asp-
for
=
"Tanggal_Lahir"
class
=
"col-sm-2 col-form-label"
>Tanggal Lahir</label>
<div
class
=
"col-sm-10"
>
@Html.TextBoxFor(model => model.Tanggal_Lahir,
new
{ @
class
=
"form-control datepicker"
, placeholder =
"Enter Date"
, id =
"Date"
, autocomplete =
"off"
})
<span asp-validation-
for
=
"Tanggal_Lahir"
class
=
"text-danger"
></span>
</div>
</div>
this is the Javascript to show calender
<script src=
"~/plugins/jquery/jquery.min.js"
></script>
<link href=
"~/JqueryCalender/jquery-ui-1.12.1.custom/jquery-ui.min.css"
rel=
"stylesheet"
/>
<script src=
"~/plugins/moment/moment.min.js"
></script>
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$(
'.datepicker'
).datepicker({
dateFormat:
"yy/mm/dd"
});
});
</script>
Reply
Answers (
8
)
12 factor, containerized applications to AWS
File Upload from Angular using REST API to SQL server database