Hi sunnyk21,
Theer is no need to create two sqlcommand. You have to just check condition with Gender from model, if the value is 3 then assign the Gender Parameters with OtherGender property else assign Gender property.
Check this example. Now please take its reference and correct your code.
Model
public class Employee
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Gender { get; set; }
public string OtherGender { get; set; }
[Required]
public string Designation { get; set; }
public List<SelectListItem> DesignationList { get; set; }
}
Controller
public class HomeController : Controller
{
string st = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public List<SelectListItem> GetDesignation()
{
List<SelectListItem> desiglist = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(st))
{
string query = "SELECT * FROM Designation";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
desiglist.Add(new SelectListItem
{
Text = rd["Designation"].ToString(),
Value = rd["DesigId"].ToString()
});
}
con.Close();
}
}
return desiglist;
}
public ActionResult Index()
{
Employee emp = new Employee();
emp.DesignationList = GetDesignation();
return View(emp);
}
[HttpPost]
public ActionResult Index(Employee emp)
{
emp.DesignationList = GetDesignation();
if (ModelState.IsValid)
{
using (SqlConnection con = new SqlConnection(st))
{
string query = "insert into EmployeeDetail(FirstName,LastName,Gender,Designation) values(@FirstName,@LastName,@Gender,@Designation)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@FirstName", emp.FirstName);
cmd.Parameters.AddWithValue("@LastName", emp.LastName);
if (emp.Gender == "3")
{
cmd.Parameters.AddWithValue("@Gender", emp.OtherGender == null ? "" : emp.OtherGender);
}
else
{
cmd.Parameters.AddWithValue("@Gender", emp.Gender);
}
cmd.Parameters.AddWithValue("@Designation", emp.Designation);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
return View(emp);
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_122860_Save_From_DropDownList_MVC.Models.Employee>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#OtherGender").hide();
$("#Gender").change(function () {
if ($("#Gender OPTION:selected").val() == "3") {
$("#OtherGender").show();
} else {
$("#OtherGender").hide();
}
});
});
</script>
</head>
<body>
<% using (Html.BeginForm("Index", "Home", FormMethod.Post)) {%>
<%:Html.AntiForgeryToken()%>
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
<%:Html.ValidationSummary(true, "", new { @class = "text-danger" })%>
<div class="form-group">
<%:Html.LabelFor(model => model.FirstName)%>
<div class="col-md-10">
<%:Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })%>
<%:Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })%>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.LastName)%>
<div class="col-md-10">
<%:Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })%>
<%:Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })%>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.Gender)%>
<div class="col-md-10">
<%:Html.DropDownListFor(model=>model.Gender,
new List<SelectListItem> {
new SelectListItem{Text="Male", Value="1"},
new SelectListItem{Text="Female", Value="2"},
new SelectListItem{Text="Other", Value="3"}
})%>
<br />
<%:Html.TextBoxFor(model=>model.OtherGender) %>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.Designation)%>
<div class="col-md-10">
<%:Html.DropDownListFor(model=>model.Designation,Model.DesignationList,"Please Select Designation")%>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
<%}%>
<div><%:Html.ActionLink("Back to List", "Index") %></div>
</body>
</html>