please help me how to validate the empcode with database values in empcode column in mvc
i want to compare the input value (empcode) compare with all values of EmpCode column in database and show an validattion message in mvc
i want to ignore the duplicate value of particular column of table and again a new value should be inserted in table in mvc
this is below code
controller
// List<Employee> ourProducts = new List<Employee>();
[HttpPost]
public JsonResult InsertEmployee(Employee Employee1, string empcode)
{
try
{
using (SivaKumarEntities entities = new SivaKumarEntities())
{
bool status = false;
const string sConnString = "Data Source=125.62.198.183;Initial Catalog=SivaKumar;UserID=SivaKumar;Password=Siva@123";
SqlConnection con = new SqlConnection(sConnString);
SqlCommand cmd = new SqlCommand("select EmpCode from Employee", con);
cmd.Parameters.AddWithValue("@EmpCode", empcode);
cmd.Connection = con;
con.Open();
status = Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ourProducts.Add(new Employee
{
EmpCode = reader["EmpCode"].ToString()
});
}
con.Close();
}
}
catch (Exception)
{
TempData["Message"] = "<script>alert('Please enter emp no Unique');</script>";
}
return Json(Employee1);
}
view
$("body").on("click", "#btnAdd", function () {
var languages = [];
$('.listvalue').each(function () {
if ($(this).is(":checked")) {
languages.push($(this).val());
}
});
var txtName = $("#EmpName");
var txtCode = $("#EmpCode");
var txtsalary = $("#salary");
var radioValue = $("input[name='gender']:checked").val();
var txtDepartment = $("#Department").find("option:selected");
var txtProject = $("#Project");
$.ajax({
type: "POST",
url: "/Home/InsertEmployee",
data: '{EmpName: "' + txtName.val() + '", EmpCode: "' + txtCode.val() + '",salary: "' + txtsalary.val() + '",Gender: "' + radioValue + '",Department: "' + txtDepartment.text() + '",Skills: "' + languages + '",Project: "' + txtProject.val() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var row = $("#WebGrid TBODY tr:last-child").clone(true);
if (row.find(".label").is(":empty")) {
$("#WebGrid TBODY tr:last-child").remove();
}
//location.reload();
}
});
});