ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 GetAllStudents() method


    (Page 3 of 5 )

    As we have agreed, this method returns an object of type StudentDataAccessCollection so let's see how we can do that. The following is the code for this method.

    public static StudentDataAccessCollection GetAllStudents(){
      try{
        using (SqlConnection connection = new SqlConnection(connString)){
          SqlCommand command = new SqlCommand("GetAllStudentsIDs", connection);
          command.CommandType = CommandType.StoredProcedure;

          StudentDataAccessCollection students = new StudentDataAccessCollection();

          connection.Open();
          using (SqlDataReader reader = command.ExecuteReader()){
            while (reader.Read()){
              students.Add(new StudentDataAccess(reader.GetInt32(0)));
            }
          }
          return students;
        }
      }
      catch (Exception ex){
        throw new ApplicationException("An error has occurred.");
      }
    }

    The method uses a SqlConnection and a SqlCommand object to execute the stored procedure named GetAllStudentsIDs, which returns only the StudentID column from the database. Note that we are not retrieving all the columns; we use the returned StudentID column's values to create the StudentDataAccess objects. As you know, the StudentDataAccess constructor accepts the studentId and communicates with the database in order to populate its fields with the values returned from the database. We accomplish this by using the following code:

    while (reader.Read()){
      students.Add(new StudentDataAccess(reader.GetInt32(0)));
    }

    We simply use the SqlDataReader.Read() method to read the StudentID column's values from the Students table, and use these values as arguments to the StudentDataAccess constructor's parameter to create the StudentDataAccess objects. Those objects are added to the StudentDataAccess collection object using the Add() method as shown above. After that we return that collection object to the caller.

    Note that the SqlDataReader.Read() method returns true if there is an available row to read, which means that its inner state is placed before the first row. To read that first row you need to call the method and test for its return value. As long as there is a next row to read it, it returns true; otherwise it returns false. Let's write code to test the method.

    More ASP.NET Articles
    More By Michael Youssef


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

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - 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





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