私のコードは次のとおりです。ブレークポイントを実行しているときにデータベースからデータを取得していますが、リストにデータが表示されますが、エラーも発生します
public static List<StudentScore> GetAllScore()
{
SqlConnection conn = MyDB.GetConnection();
string selectStm = "SELECT en.CourseID,en.Score,s.StudentID FROM EnrollmentTable en,Student s WHERE en.StudentID = s.StudentID";
SqlCommand command = new SqlCommand(selectStm, conn);
List<StudentScore> aStudentScore = new List<StudentScore>();
try
{
conn.Open();
SqlDataReader reader = command.ExecuteReader();
Console.WriteLine(reader.HasRows.ToString());
while (reader.Read())
{
StudentTable st = new StudentTable();
CourseTable cr = new CourseTable();
Enrollment enr = new Enrollment();
StudentScore score = new StudentScore();
enr.CourseData = cr;
enr.StudentData = st;
//score.EnrollmentData.StudentData.StudentID = reader["StudentID"].ToString();
//score.EnrollmentData.CourseData.CourseID = reader["CourseID"].ToString();
st.StudentID = reader["StudentID"].ToString();
cr.CourseID = reader["CourseID"].ToString();
score.Score = Convert.ToInt32(reader["Score"]);
score.EnrollmentData = enr;
aStudentScore.Add(score);
}
reader.Close();
return aStudentScore;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
}
データベースからデータを取得しますが、このエラーが表示されます.....オブジェクトは DBNull から他の型にキャストできません。修正方法を教えてください。