public List <Student> GetStudents() { MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT students.* FROM courses JOIN course_info ON (courses.course_id = course_info.course_id) JOIN students ON (course_info.student_id = students.student_id) WHERE courses.course_id = @CourseId;"; MySqlParameter courseIdParameter = new MySqlParameter(); courseIdParameter.ParameterName = "@CourseId"; courseIdParameter.Value = _course_id; cmd.Parameters.Add(courseIdParameter); var rdr = cmd.ExecuteReader() as MySqlDataReader; List <int> studentIds = new List <int> { }; while (rdr.Read()) { int studentId = rdr.GetInt32(0); studentIds.Add(studentId); } rdr.Dispose(); List <Student> students = new List <Student> { }; foreach (int studentId in studentIds) { var studentQuery = conn.CreateCommand() as MySqlCommand; studentQuery.CommandText = @"SELECT * FROM students WHERE student_id = @StudentId;"; MySqlParameter studentIdParameter = new MySqlParameter(); studentIdParameter.ParameterName = "@StudentId"; studentIdParameter.Value = studentId; studentQuery.Parameters.Add(studentIdParameter); var studentQueryRdr = studentQuery.ExecuteReader() as MySqlDataReader; while (studentQueryRdr.Read()) { int thisStudentId = studentQueryRdr.GetInt32(0); string studentName = studentQueryRdr.GetString(1); string studentDate = studentQueryRdr.GetString(2); Student foundStudent = new Student(studentName, studentDate, thisStudentId); students.Add(foundStudent); } studentQueryRdr.Dispose(); } conn.Close(); if (conn != null) { conn.Dispose(); } return(students); }