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 innformation 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 WebServer
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 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 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…