I create 3 classes then those 3 classes’s object create in another class. Now I want to read the data from the database.
My code is: I am retrieving the data from the database and call from the combo box.
SqlConnection conn = MyDB.GetConnection();
string selectStm = "SELECT c.CourseID,en.Score,s.StudentID FROM EnrollmentTable en,Course c,Student s WHERE en.StudentID = s.StudentID AND en.CourseID = c.CourseID";
SqlCommand command = new SqlCommand(selectStm, conn);
//command.Parameters.AddWithValue("@StudentID", StudentID);
List<StudentScore> aStudentScore = new List<StudentScore>();
StudentScore score = null;
try
{
conn.Open();
SqlDataReader reader = command.ExecuteReader();
Enrollment enr = new Enrollment();
while (reader.Read())
{
score = new StudentScore();
score.EnrollmentData.StudentData.StudentID = reader["StudentID"].ToString();//Here is the problem.
score.EnrollmentData.CourseData.CourseID = reader["CourseID"].ToString();//Here is the problem.
score.Score = Convert.ToInt32(reader["Score"]);
aStudentScore.Add(score);
}
reader.Close();
return aStudentScore;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
It doesn’t take any value….How can I do it?
If your
SELECTstatement is not returning any rows, then there is a problem with your data. Either theEnrollmentTable.StudentIDdoesn’t match any records in theStudenttable orEnrollmentTable.CourseIDdoesn’t match any courses in theCoursetable.In any case, I tried to simplify the code so it would be easier to read: