ASP.NET
  Home arrow ASP.NET arrow Page 4 - Developing Methods for the StudentDB Class...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Developing Methods for the StudentDB Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-09-11

    Table of Contents:
  • Developing Methods for the StudentDB Class for ASP.NET 2.0
  • Creating the INSERT, UPDATE and DELETE methods
  • Explaining the code for the methods
  • Testing the Data Access Methods
  • The complete code for the Student and StudentDB Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Michael Youssef


       · Today we complete the StudentDB class, you need to read the previous 2 articles to ...
       · i used your code in my application and it's nice. thank you very much for the...
       · It would be better to use the code with the ObjectDataSource control and using a...
     

    ASP.NET ARTICLES

    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT