Словарь параметров содержит нулевую запись для параметра «id» необнуляемого типа «System.Int32» для метода «CumulativeP1». Models.Teacher FindTeacher(Int32)» в «CumulativeP1.Controllers.TeacherDataController». Необязательный параметр должен быть ссылочного типа, типа, допускающего значение NULL, или быть объявлен как необязательный параметр.
Код: Выделить всё
teacheridAPI-контроллер:
Код: Выделить всё
[HttpGet]
public List 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 Teachers = new List();
// 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;
}
Код: Выделить всё
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; }
}
Код: Выделить всё
// GET: teacher/show/{id}
public ActionResult Show(int id)
{
TeacherDataController controller = new TeacherDataController();
Teacher NewTeacher = controller.FindTeacher(id);
return View(NewTeacher);
}
Код: Выделить всё
@model CumulativeP1.Models.Teacher
@{
ViewBag.Title = "Show";
}
Show Teachers
@Model.TeacherName
Код: Выделить всё
Command.CommandText = "SELECT * FROM teachers WHERE teacherid = "+id;
Подробнее здесь: https://stackoverflow.com/questions/787 ... h-sql-data
Мобильная версия