Hi trisetia302,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE Patient
(
Id INT IDENTITY PRIMARY KEY,
Name_Patient VARCHAR(100),
Address VARCHAR(500),
Gender VARCHAR(10),
BirthDate DATETIME,
Status VARCHAR(10)
)
Model
using System.ComponentModel.DataAnnotations;
public class PatientModel
{
[Required]
public string Name { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Gender { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true, NullDisplayText = "NULL")]
public Nullable<System.DateTime> BirthDate { get; set; }
[Required]
public string Status { get; set; }
}
Namespaces
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
Controller
public class HomeController : Controller
{
private IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(PatientModel patientModel)
{
if (ModelState.IsValid)
{
using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("MyConn")))
{
string query = "Insert Into Patient (Name_Patient,Address,Gender,BirthDate,Status) Values (@Name,@Address,@Gender,@BirthDate,@Status)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", patientModel.Name);
cmd.Parameters.AddWithValue("@Address", patientModel.Address);
cmd.Parameters.AddWithValue("@Gender", patientModel.Gender);
if (patientModel.BirthDate == null)
{
cmd.Parameters.AddWithValue("@BirthDate", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@BirthDate", patientModel.BirthDate);
}
cmd.Parameters.AddWithValue("@Status", patientModel.Status);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index", "Home");
}
}
}
else
{
return View(patientModel);
}
}
}
View
@model Date_Insert_MVC_Core.Models.PatientModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.datepicker').datepicker({
dateFormat: "yy/mm/dd"
});
});
</script>
</head>
<body class="container">
<div class="row">
<div class="col-md-4">
<form asp-action="Index" asp-controller="Home" method="post">
<div class="form-group row">
<label asp-for="Name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control", placeholder = "Enter Name", id = "Name", autocomplete = "off" })
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="Address" class="col-sm-2 col-form-label">Address</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Address, new { @class = "form-control", placeholder = "Enter Address", id = "Address", autocomplete = "off" })
<span asp-validation-for="Address" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="Gender" class="col-sm-2 col-form-label">Gender</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Gender, new { @class = "form-control", placeholder = "Enter Gender", id = "Gender", autocomplete = "off" })
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="BirthDate" class="col-sm-2 col-form-label">Date Of Birth</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.BirthDate, new { @class = "form-control datepicker", placeholder = "Enter Date", id = "Date", autocomplete = "off" })
<span asp-validation-for="BirthDate" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="Status" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Status, new { @class = "form-control", placeholder = "Enter Status", id = "Status", autocomplete = "off" })
<span asp-validation-for="Status" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<input type="submit" value="Insert" class="btn btn-default" />
</div>
</form>
</div>
</div>
</body>
</html>
Inserted Record