ASP.NET
  Home arrow ASP.NET arrow Page 4 - Adding Methods to the StudentDataAccess Cl...
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

Adding Methods to the StudentDataAccess Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-18

    Table of Contents:
  • Adding Methods to the StudentDataAccess Class for ASP.NET 2.0
  • The InsertNewStudent() and UpdateStudent() methods
  • Testing the methods from a Web page
  • Creating the DeleteStudent() method
  • The StudentDataAccess class, the complete code

  • 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


    Adding Methods to the StudentDataAccess Class for ASP.NET 2.0 - Creating the DeleteStudent() method


    (Page 4 of 5 )

    We have provided the functionality of selecting, inserting and updating records from the Students table. Now we need to provide the capability of deleting records. Add the following DeleteStudent() method to the StudentDataAccess class:

    public void DeleteStudent()
    {
    try
    {
    if (this.studentId != -1)
    {
    using (SqlConnection connection = new SqlConnection(connString))
    {
    SqlCommand command = new SqlCommand("DeleteStudent",
    connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@StudentID", SqlDbType.Int).Value =
    this.studentId;
    connection.Open();
    int rowsAffected = command.ExecuteNonQuery();
    if (rowsAffected == 1)
    {
    this.studentId = -1;
    this.firstName = String.Empty;
    this.lastName = String.Empty;
    this.major = String.Empty;
    this.dateOfBirth = DateTime.MinValue;
    this.admissionDate = DateTime.MinValue;
    this.active = false;
    }
    }
    }
    }
    catch (Exception ex)
    {
    throw new ApplicationException("An error has occurred.");
    }

    }

    The DeleteStudent() method executes the DeleteStudent stored procedure which takes the studentId field as a value to its @StudentID parameter. Note that we check whether the record has been deleted from the database through the ExecuteNonQuery() method, and if so we set the studentId field to -1 and the rest of the private fields to values other than the matching student record. To test the method, replace the code in the Page_Load() event handler of the Default.aspx 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 />");
    if (student.Active == false)
    student.DeleteStudent();
    }

    Response.Write("After deleting inactive students, we have the
    following students in the database <br />");

    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 />");
    }

    The result of running the page would be:

    Student ID: 1
    Student First Name: Jack
    Student Last Name: Roberts
    Student Date Of Birth: 2/15/1984 12:00:00 AM
    Student Admission Date: 7/7/2006 12:00:00 AM
    Student Major: Computer Science
    Student Active: True

    Student ID: 2
    Student First Name: Mary
    Student Last Name: Paul
    Student Date Of Birth: 5/19/1984 12:00:00 AM
    Student Admission Date: 7/7/2006 12:00:00 AM
    Student Major: Information Systems
    Student Active: True

    Student ID: 3
    Student First Name: Mark
    Student Last Name: David
    Student Date Of Birth: 8/6/1984 12:00:00 AM
    Student Admission Date: 7/7/2006 12:00:00 AM
    Student Major: Physics
    Student Active: False

    Student ID: 4
    Student First Name: Julia
    Student Last Name: Anderson
    Student Date Of Birth: 3/27/1983 12:00:00 AM
    Student Admission Date: 7/10/2006 12:00:00 AM
    Student Major: Computer Science
    Student Active: True

    Student ID: 9
    Student First Name: Jackson
    Student Last Name: Robin
    Student Date Of Birth: 11/5/1982 12:00:00 AM
    Student Admission Date: 8/25/2007 10:14:05 AM
    Student Major: Computer Science
    Student Active: False

    After deleting inactive students, we have the following students in the database:

    Student ID: 1
    Student First Name: Jack
    Student Last Name: Roberts
    Student Date Of Birth: 2/15/1984 12:00:00 AM
    Student Admission Date: 7/7/2006 12:00:00 AM
    Student Major: Computer Science
    Student Active: True

    Student ID: 2
    Student First Name: Mary
    Student Last Name: Paul
    Student Date Of Birth: 5/19/1984 12:00:00 AM
    Student Admission Date: 7/7/2006 12:00:00 AM
    Student Major: Information Systems
    Student Active: True

    Student ID: 4
    Student First Name: Julia
    Student Last Name: Anderson
    Student Date Of Birth: 3/27/1983 12:00:00 AM
    Student Admission Date: 7/10/2006 12:00:00 AM
    Student Major: Computer Science
    Student Active: True

    What we did is call the DeleteStudent() method on StudentDataAccess objects that have false as their value to the property Active, in the foreach statement. Also you might test the DeleteStudent() method with the following code:

    StudentDataAccess student = new StudentDataAccess(4);
    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 />");

    student.DeleteStudent();
    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 the following result:

    As you can see, we have deleted the student Julia, and when we printed out the values of the properties again we found that all the information for Julia is gone. I think that the StudentDataAccess class is complete by now so let's take a look at it.

    More ASP.NET Articles
    More By Michael Youssef


       · Ok by now we have a complete data access class. It's also good to read the 3...
     

    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 6 hosted by Hostway
    Stay green...Green IT