Error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'CumulativeP1.Models.Teacher FindTeacher(Int32)' in 'CumulativeP1.Controllers.TeacherDataController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
teacherid
- int, primary key in SQL.
API controller:
[HttpGet]
public List<Teacher> ListTeachers()
{
// Create an instance of a connection
MySqlConnection Connection = School.AccessDatabase();
// Open the connection between the web server and database
Connection.Open();
// Establish a new command (query) for our database
MySqlCommand Command = Connection.CreateCommand();
// SQL query
Command.CommandText = "Select * from teachers";
// Gather result set of query into a variable
MySqlDataReader ResultSet = Command.ExecuteReader();
// Create an empty list of Teacher Names
List<Teacher> Teachers = new List<Teacher>();
// Loop through each row the result set
while (ResultSet.Read())
{
// Access column information by the DB column name as an index
string TeacherName = ResultSet["teacherfname"] + " " + ResultSet["teacherlname"];
string EmployeeNumber = Convert.ToString(ResultSet["employeenumber"]);
decimal TeacherSalary = Convert.ToDecimal(ResultSet["salary"]);
DateTime TeacherHireDate = Convert.ToDateTime(ResultSet["hiredate"]);
Teacher NewTeacher = new Teacher();
NewTeacher.TeacherName= TeacherName;
NewTeacher.TeacherSalary= TeacherSalary;
NewTeacher.TeacherHireDate= TeacherHireDate;
NewTeacher.EmployeeNumber= EmployeeNumber;
// Add the Teacher elements to the List
Teachers.Add(NewTeacher);
}
// Close the connection between the MySQL database and the web server
Connection.Close();
// Return the final list of author names
return Teachers;
}
[HttpGet]
public Teacher FindTeacher(int id)
{
Teacher NewTeacher = new Teacher();
// Create an instance of a connection
MySqlConnection Connection = School.AccessDatabase();
// Open the connection between the web server and database
Connection.Open();
// Establish a new command (query) for our database
MySqlCommand Command = Connection.CreateCommand();
// SQL query
Command.CommandText = "SELECT * FROM teachers WHERE teacherid = "+id;
// Gather result set of query into a variable
MySqlDataReader ResultSet = Command.ExecuteReader();
while (ResultSet.Read())
{
string TeacherName = ResultSet["teacherfname"] + " " + ResultSet["teacherlname"];
string EmployeeNumber = Convert.ToString(ResultSet["employeenumber"]);
decimal TeacherSalary = Convert.ToDecimal(ResultSet["salary"]);
DateTime TeacherHireDate = Convert.ToDateTime(ResultSet["hiredate"]);
NewTeacher.TeacherName = TeacherName;
NewTeacher.TeacherSalary = TeacherSalary;
NewTeacher.TeacherHireDate = TeacherHireDate;
NewTeacher.EmployeeNumber = EmployeeNumber;
}
return NewTeacher;
}
My model class:
public class Teacher
{
// we use this class to describe what teachers' fields are for other components
public string TeacherName { get; set; }
public string EmployeeNumber { get; set; }
public decimal TeacherSalary { get; set; }
public DateTime TeacherHireDate { get; set; }
public int TeacherID { get; set; }
}
My ASP.NET MVC controller:
// GET: teacher/show/{id}
public ActionResult Show(int id)
{
TeacherDataController controller = new TeacherDataController();
Teacher NewTeacher = controller.FindTeacher(id);
return View(NewTeacher);
}
Haven't started to work on html, very simple now.
@model CumulativeP1.Models.Teacher
@{
ViewBag.Title = "Show";
}
<h2>Show Teachers</h2>
@Model.TeacherName
I suppose the problem is about teacherid
, is my code right in getting SQL data?
Command.CommandText = "SELECT * FROM teachers WHERE teacherid = "+id;
Not sure why the error message indicating null parameter, teacherid
is the primary key in the database...
route configuration
looks like? Is that correctly set up to map the id parameter properly? What's the details ofTeacherDataController()
?id
correct in your controller action and pass toFindTeacher
function.