Adding Methods to the StudentDataAccess Class for ASP.NET 2.0 - Testing the methods from a Web page
(Page 3 of 5 )
Write the following code inside the Page_Load() event handler method of the page Default.aspx:
StudentDataAccess newStudent = new StudentDataAccess();
newStudent.FirstName = "Jackson";
newStudent.LastName = "Robin";
newStudent.Major = "Information Systems";
newStudent.DateOfBirth = DateTime.Parse("11/5/1982");
newStudent.AdmissionDate = DateTime.Now;
newStudent.Active = true;
newStudent.SaveStudent();
int newStudentId = newStudent.StudentId;
StudentDataAccess student = new StudentDataAccess(newStudentId);
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 />");
newStudent.Major = "Computer Science";
newStudent.Active = false;
newStudent.SaveStudent();
student = new StudentDataAccess(newStudentId);
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 />");
Now run the page and you will get the result shown below.

We created a StudentDataAccess object and assigned values to its properties. Then we called the SaveStudent() method, and because this is a new student those values are inserted into the Students table, by calling the private InsertNewStudent() method. After that we store the StudentId in a local variable and pass it to the StudentDataAccess constructor to create a populated object from that StudentId, as we have seen before. We then use the Response.Write() method to print out the values of the properties of that new object.
Next we update the Major and the Active properties of the the newStudent object, and call its SaveStudent() method. Because the newStudent.StudentId property's value has a valid studentId value, this call to the SaveStudent() method will update the record by calling the private UpdateStudent() method. Again we check the updates through creating another StudentDataAccess object with that studentId value and print out its values.
Next: Creating the DeleteStudent() method >>
More ASP.NET Articles
More By Michael Youssef