ASP.NET
  Home arrow ASP.NET arrow Page 2 - Improving the StudentDataAccess Class for ...
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 
Dedicated Servers 
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

Improving 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 / 3
    2007-09-17

    Table of Contents:
  • Improving the StudentDataAccess Class for ASP.NET 2.0
  • Creating the StudentData AccessCollection class
  • Creating the GetAllStudents() method
  • Testing the GetAllStudents() method
  • Modifying the application to retrieve all the columns

  • 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


    Improving the StudentDataAccess Class for ASP.NET 2.0 - Creating the StudentData AccessCollection class


    (Page 2 of 5 )

    In the next section we are going to create a method called GetAllStudents() which returns all the rows of the Students table as objects of type StudentDataAccess in a collection object, which we will create in this section, of type StudentDataAccessCollection. The method signature looks like this

     public static StudentDataAccessCollection GetAllStudents()

    Let's create the StudentDataAccessCollection class. Right click on the App_Code folder, click on Add new Item menu option, select a class template, name it StudentDataAccessCollection.cs and click on OK. Replace the auto-generated code of the class file with the following code.

    using System;
    using System.Collections;


    public class StudentDataAccessCollection{
      private ArrayList list;

      public ArrayList List{
        get { return list; }
      }

      public StudentDataAccessCollection(){
        this.list = new ArrayList();
      }

      public void Add(StudentDataAccess student){
        this.list.Add(student);
      }

      public void Remove(StudentDataAccess student){
        this.list.Remove(student);
      }

      public int Count{
        get { return this.list.Count; }
      }

      public StudentDataAccess this[int index]{
        get { return (StudentDataAccess)this.list[index]; }
      }

      public IEnumerator GetEnumerator(){
        return list.GetEnumerator();
      }
    }

    You might think that we can write the signature of the GetAllStudents() method to return a .NET Collection object as follows, instead of creating our own collection class.

     public static ArrayList GetAllStudents()

    Yes, you can do that, but the problem is that the ArrayList.Add() method accepts the general object data type as a parameter, so it can contain any data type. We need a way to have a collection object with an Add() method that accepts the data type we are working with. To make this possible we have defined a private ArrayList that will be used for storing our objects.

    We have defined the ArrayList object as private and created methods, like Add(), as public with the data type we want to work with, in this case the StudentDataAccess type, as the parameter. Inside those public methods we pass the StudentDataAccess object as a value to the ArrayList.Add() method. Using this technique we guarantee that the StudentDataAccessCollection.Add() method will add only objects of type StudentDataAccess to the inner private ArrayList object. Also using this technique you can write code that is strongly typed, as we are going to see in the testing page.

    The same principle applies to the Remove() method, the property Count and the indexer. We simply choose what we want to provide as part of our collection's functionality by implementing public methods and properties, that use the private ArrayList methods and properties, for storing and retrieving our strongly typed objects.

    To use a foreach statement with a StudentDataAccessCollection object you must provide a public method with the following signature:

    public IEnumerator GetEnumerator()

    It's used by the foreach statement to iterate over the objects of the collection. we simply called the ArrayList.GetEnumerator() to return the enumerator for us. We didn't do anything new other than use the private ArrayList object's methods and properties. Create a new class file in the App_Code folder, name it StudentDataAccessCollection.cs and replace the class template code with the above class definition.

    Let's write the GetAllStudents() method and test it.

    More ASP.NET Articles
    More By Michael Youssef


       · Make sure that you read the article "Building the StudentDataAccess Class for...
     

    ASP.NET ARTICLES

    - 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 ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway