SunQuest
 
       ASP.NET
  Home arrow ASP.NET arrow Improving the StudentDataAccess Class for ...
Iron Speed
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Improving the StudentDataAccess Class for ASP.NET 2.0


    (Page 1 of 5 )

    Today we continue developing the StudentDataAccess class we started with in the first part of this series. We will create another static method called GetAllStudents() which returns all the records from the Students table as a StudentDataAccessCollection object, which we will create first.

    If you read my article Testing the StudentDB Class for ASP.NET 2.0, you saw that we used C# generics as the return type of the method GetAllStudentsInCollection() to return a strongly typed collection of Student objects. The method looks like this:

    public static List<Student> GetAllStudentsInCollection(){
      try{
        using (SqlConnection connection = new SqlConnection
    (connString)){
          SqlCommand command = new SqlCommand("GetAllStudents",
    connection);
          command.CommandType = CommandType.StoredProcedure;

          List<Student> students = new List<Student>();

          connection.Open();
          using (SqlDataReader reader = command.ExecuteReader()){
            while (reader.Read()){
              Student student;
              student = new Student((int)reader["StudentID"], reader
    ["FirstName"].ToString(),
              reader["LastName"].ToString(), (DateTime)reader
    ["DateOfBirth"],
    (DateTime)reader["AdmissionDate"], reader["major"].ToString(),
    (bool)reader["active"]);
              students.Add(student);
            }
          }
          return students;
        }
      }
      catch (SqlException ex){
        throw new ApplicationException("A database error has
    occurred.");
      }
      catch (Exception ex){
        throw new ApplicationException("An error has occurred.");
      }
    }

    Using generics is very easy and saves time, but you might be new to this technique; you might have not created a custom collection class by hand before. I want to show you what we will have to do without generics. If you are not familiar with generics please consult other articles because it's an important subject for developing .NET applications.

    In the next section I will show you an alternative to using generics. We will create a class called StudentDataAccessCollection that we will use to store objects of type StudentDataAccess which represent records in the Students table. So you might retrieve all the records from the Students table, use them to create StudentDataAccess objects, and add those objects to an instance of type StudentDataAccessCollection. If you do that you will be able to execute code that uses a foreach statement -- for example, to iterate through the StudentDataAccess objects in the StudentDataAccessCollection and use their properties as we are going to see soon.

    More ASP.NET Articles
    More By Michael Youssef


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

    ASP.NET ARTICLES

    - 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...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...

    Iron Speed




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