Improving the StudentDataAccess Class for ASP.NET 2.0 - Testing the GetAllStudents() method
(Page 4 of 5 )
Replace the code that you have written in the Page_Load() event handler with the following:
StudentDataAccessCollection students = StudentDataAccess.GetAllStudents();
foreach (StudentDataAccess student in students){
Response.Write("<b>Student ID:<b/> " + student.StudentId +
"<br />" +
"<b>Student First Name:<b/> " + student.FirstName + "<br />" +
"<b>Student Last Name:<b/> " + student.LastName + "<br />" +
"<b>Student Date Of Birth:<b/> " + student.DateOfBirth + "<br />"
+
"<b>Student Admission Date:<b/> " + student.AdmissionDate +
"<br />" +
"<b>Student Major:<b/> " + student.Major + "<br />" +
"<b>Student Active:<b/> " + student.Active + "<br />"
);
Response.Write("<br />");
}
Run the page and you will get all the records of the Students table.

We assigned the return object to calling the GetAllStudents() method to an object called students of type StudentDataAccessCollection, then we used a foreach statement to iterate over the StudentDataAccess objects of that students collection and printed out the values of the fields. As you can see, we have accessed each object of the StudentDataAccessCollection in a strongly typed syntax as in student.FirstName and student.LastName.
Note that to return these StudentDataAccess objects we have communicated with the database many times. The first communication happened when we retrieved the StudentID column's values and the other four communications (because we have four records in the database) took place when we passed the StudentID column's values to the StudentDataAccess constructor to populate the object's fields from the database. This is fine for our example, but imagine that you are executing this code against a database with thousands of records; it would slow down the application's performance. To solve this problem, move on to the next section to see how we handle this situation.
Next: Modifying the application to retrieve all the columns >>
More ASP.NET Articles
More By Michael Youssef