I'm building web application using ASP.NET framework, and when I'm trying to create new object this message appear.
Now i saw in this (What is a NullReferenceException, and how do I fix it? )thread couple options:
- the constructor is null, but in my case he is not null.
- View return nothing, but my view return ClassesForm.
All the rest of the functions works great, like edit, delete, Details. but for some reason "create" does this problem. What I'm missing here?
this is the code.
The constructors:(ClassesModel.cs)
public class ClassesModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Building { get; set; }
[Required]
public string How_Many_In_Class { get; set; }
public ClassesModel()
{
Id = -1;
Name = "empty";
Building = "empty";
How_Many_In_Class = "empty";
}
public ClassesModel(int id, string name, string building, string how_Many_In_Class)
{
Id = id;
Name = name;
Building = building;
How_Many_In_Class = how_Many_In_Class;
}
}
the conctoller:(ClassesController.cs)
public ActionResult Create()
{
return View("ClassesForm");
}
[HttpPost]
public ActionResult ProcessCreate(ClassesModel classesModel)
{
//save to DB
ClassDAO classesDAO = new ClassDAO();
classesDAO.CreateOrUpdate(classesModel);
return View("Details", classesModel);
}
the SQL insert:( ClassDAO.cs)
public int CreateOrUpdate(ClassesModel classesModel)
{
//access DB
using (SqlConnection connection = new SqlConnection(connectionString))
{
string SqlQuery = "";
// if classesModel.id <=1then create
if (classesModel.Id <= 0)
{
SqlQuery = "INSERT INTO dbo.Classes Values(@Name,@Building,@How_Many_In_Class)";
}
else
{ // if classesModel.id>1then update is meant.
SqlQuery = "UPDATE dbo.Classes SET Name = @Name,Building = @Building, How_Many_In_Class = @How_Many_In_Class WHERE Id = @Id";
}
SqlCommand Command = new SqlCommand(SqlQuery, connection);
Command.Parameters.Add("@Id", System.Data.SqlDbType.VarChar, 1000).Value = classesModel.Id;
Command.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 1000).Value = classesModel.Name;
Command.Parameters.Add("@Building", System.Data.SqlDbType.VarChar, 1000).Value = classesModel.Building;
Command.Parameters.Add("@How_Many_In_Class", System.Data.SqlDbType.VarChar, 1000).Value = classesModel.How_Many_In_Class;
connection.Open();
int NewID= Command.ExecuteNonQuery();
return NewID;
}
}
and the ClassesForm where the problem is:ClassesForm.cshtml
@model ClasSaver.Models.ClassesModel
@{
ViewBag.Title = "ClassesForm";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ClassesForm</h2>
@using (Html.BeginForm("ProcessCreate", "Classes"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ClassesModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Building, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Building, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Building, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.How_Many_In_Class, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.How_Many_In_Class, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.How_Many_In_Class, "", new { @class = "text-danger" })
</div>
</div>
<input type="hidden" name="Id" value="@Model.Id"/>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}