My code is :
I am retrieving the data fron database when i am doing the breakpoint it show the data in list,but it also give me a error
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();
}
}
}
}
It takes a data from database but show mw a this error…..Object cannot be cast from DBNull to other types so what it mean please tell me how to fix it?
you need to check if the reader is of type DBNULL
Call IsDBNull() on the reader to check the column before attempting to convert it:
Or, compare against DBNull.Value: