Developing Methods for the StudentDB Class for ASP.NET 2.0 - Testing the Data Access Methods
(Page 4 of 5 )
First of all create another constructor overload in the Student class which doesn't accept the Student ID value, as in the following code:
public Student(string firstName, string lastName,
DateTime dateOfBirth, DateTime admissionDate, string major, bool active){
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.admissionDate = admissionDate;
this.major = major;
this.active = active;
}
This constructor is used when we want to insert a new student record, because the StudentID column is being generated on the server, and it doesn't make any sense to pass a value for it. Anyway, if your code uses the first constructor overload that accepts the studentId it wouldn't do anything with it. It will generate the StudentID on the server because the column is defined as IDENTITY.
The following is the code of the Page_Load() event handler that is needed to call and test the three methods we have created in this article.
protected void Page_Load(object sender, EventArgs e){
// creating a new Student object
Student newStudent = new Student("Suzan", "Chris",
DateTime.Parse("11/2/1983"),
DateTime.Parse("3/8/2006"), "Information Systems", true);
// using the InsertStudent method
int studentId = StudentDB.InsertStudent(newStudent);
Response.Write("The student with the ID of " + studentId + "
has been inserted into the database");
// retrieving the newly inserted student record
Student student = StudentDB.GetStudent(studentId);
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 />");
// updating the newly inserted student record
student.FirstName = "Suzane";
student.Major = "Computer Science";
// submitting the changes to the database through
the UpdateStudent() method
Response.Write("Updating the first name and the major of the
student");
StudentDB.UpdateStudent(student);
// retrieving the newly updated student record
student = StudentDB.GetStudent(studentId);
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 />");
// deleting the new student record
int rowsAffected = StudentDB.DeleteStudent(student.StudentId);
if (rowsAffected == 1)
Response.Write("The Student with the Student ID of " +
studentId + " has been deleted from the database");
}
When you run the page it will look like the following screen shot.

We have inserted, updated and deleted a record from the Students table. The code creates the newStudent object which uses the second constructor overload to initialize the object without assigning a value to the studentId field. It makes sense because this value is generated by the database, and whatever value you are going to pass to the constructor will be ignored, because our InsertStudent stored procedure doesn't have an input parameter for studentId; it only has an output parameter. So even if you called the first constructor and passed a value like 100 or even 0 it will not affect the update operation, but it may break your application if you depend on this value. We are going to discuss a better technique for solving this problem later.
We then call the StudentDB.InsertStudent() method and pass the newStudent object as a value to its parameter. The method returns the newly inserted StudentID value which we assign to the local variable studentId. We then use the StudentDB.GetStudent() method to print out this newly inserted record; we simply pass the studentId value of the newly inserted record to it. After that we change the values of some of the properties of the returned Student object, namely the FirstName and the Major properties.
The next step is to call the method UpdateStudent() which accepts a student object and updates its record in the database. We print out the updated record again and finally we delete the record. We pass the student.StudentId property as a value to the DeleteStudent() method's parameter. This method returns the number of rows that have been affected by the delete operation. Then we test the return value and if it is equal to 1, which means that 1 record with a matching student.StudentId has been deleted, we print out an appropriate message to the user.
I think by now you have a clear idea of how to create stored procedures, create a Data Access class and represent records in object-oriented classes. In the next three-article series I will show you another, and better, technique for doing the same database operations with an object-oriented Data Access class.
If you need the full code of the Student and the StudentDB classes go to the next section.
Next: The complete code for the Student and StudentDB Classes >>
More ASP.NET Articles
More By Michael Youssef