Hello @everyone,
I am developing an Asp.Net MVC application without Entity Framework using Ado.Net code.
On Create view submit button click my Model is becoming null and throws an exception i.e object reference not set to an instance of an object.
I have searched for this but unable to figure it out with my code.
Kindly help me getting out of this. Any suggestions will be appriciated.
Below is my code.
Thanks & Regards
public class Team
{
public Team()
{
this.departments = new List<Department>();
this.Employees = new List<Registration>();
}
public int Id { get; set; }
public string team_Name { get; set; }
public string team_Code { get; set; }
public string department_Id { get; set; }
[NotMapped]
public IEnumerable<Department> departments { get; set; }
public string registration_Id { get; set; }
[NotMapped]
public IEnumerable<Registration> Employees { get; set; }
public string description { get; set; }
public IEnumerable<Registration> member_Id { get; set; }
public string[] memberList { get; set; }
[NotMapped]
public IEnumerable<Registration> TeamLead { get; set; }
public string service_Id { get; set; }
[NotMapped]
public IEnumerable<Service> Services { get; set; }
public string[] serviceList { get; set; }
}
@model WebCRM.Models.Team
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_MasterLayout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Team</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.team_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.team_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.team_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.team_Code, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.team_Code, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.team_Code, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.department_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (Model != null)
{
@Html.DropDownListFor(model => model.department_Id, new SelectList(Model.departments, "DeptId", "DepartmentName"), "--Select Department--", new { id = "ddlDepartment", @class = "form-control" })
@Html.ValidationMessageFor(model => model.department_Id, "", new { @class = "text-danger" })
}
</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>
@section scripts{
<script>
$(function () {
$("#ddlDepartment, #ddlTeamLeader, #ddlMembers, #ddlServices").chosen({
disable_search_threshold : 5
});
$("#lbTeamLeader").chosen();
});
</script>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
public class TeamController : Controller
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["dbcs"].ToString());
SqlCommand cmd;
SqlDataReader sdr;
DataTable dt;
SqlDataAdapter sda;
bindDDL bd = new bindDDL();
// GET: Team
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
Team t = new Team();
t.departments = bindDepartments();
t.TeamLead = bindEmpWthDesignation();
t.Employees = bindEmp();
t.Services = bindServices();
return View(t);
}
[HttpPost]
public ActionResult Create(Team team)
{
if (ModelState.IsValid)
{
cmd = new SqlCommand("Insert into tbl_Team(team_Name, team_Code, department_Id, registration_Id, description) values (@team_Name, @team_Code, @department_Id, @registration_Id, @description)", con);
cmd.Parameters.AddWithValue("@team_Name", team.team_Name);
cmd.Parameters.AddWithValue("@team_Code", team.team_Code);
cmd.Parameters.AddWithValue("@department_Id", Convert.ToInt32(team.department_Id));
cmd.Parameters.AddWithValue("@registration_Id", Convert.ToInt32(team.registration_Id));
cmd.Parameters.AddWithValue("@description", team.description);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
con.Close();
team.Id = getTeamId();
i = saveMembers(team);
if (i > 0)
{
i = saveTeamServices(team);
if (i > 0)
{
return View();
}
}
}
}
return View();
}
}