ASP.NET
  Home arrow ASP.NET arrow Building the StudentDataAccess Class for A...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

Building 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 / 6
    2007-09-12

    Table of Contents:
  • Building the StudentDataAccess Class for ASP.NET 2.0
  • Introducing the StudentDataAccess class
  • Creating the Constructor
  • Testing the Constructor

  • 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


    Building the StudentDataAccess Class for ASP.NET 2.0


    (Page 1 of 4 )

    Confused about the difference between using generics and using a custom collection object? This three-part series will clarify the difference for you, while showing you how to create a single class that represents a student and its associated database operations.

    If you read my series Creating the StudentDB Class for ASP.NET 2.0 you know that we have used the Student class to represent a record in the Students table of the School database we have created, and we also have used static methods in the StudentDB class to provide the SELECT, INSERT, UPDATE and DELETE operations.

    In this article, we are going to create only one class that represents a student and its associated database operations. We are going to create the StudentDataAccess class. We are also going to create a class called StudentDataAccessCollection class to represent a collection of students.

    In the previous article series we used C# generics to return a strongly typed collection as a result of calling the GetAllStudentsInCollection() method. In this series, we will create a simple collection, by hand, to clarify the difference between using generics and using a custom collection object. It's recommended that you use generics but we show how you can create that custom StudentDataAccessCollection class in case you haven't done it before.

    If you didn't read my series "Creating the StudentDB Class for ASP.NET 2.0," you can read the first section of this article to create the database table and the stored procedures and continue reading the article and the next two parts. Then you can read the aforementioned series of articles about the StudentDB and understand the difference between using the two techniques.

    The StudentDataAccess class has private members, public properties, static methods, instance methods and constructors. We will create them all in this series, also we are going to test those methods from an ASP.NET web page.

    We start by creating the Students table, in a database called School. We insert some records in this Table and create the stored procedures needed by the StudentDataAccess class. Run the following T-SQL code into your SQL Server Management Studio to create the necessary database, the table and the stored procedures.

    CREATE DATABASE School
    GO
    USE School
    GO
    CREATE TABLE Students
    (
    StudentID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    FirstName NVARCHAR(20) NOT NULL,
    LastName NVARCHAR(20) NOT NULL,
    DateOfBirth DATETIME NOT NULL,
    AdmissionDate DATETIME NOT NULL,
    Major NVARCHAR(40) NOT NULL,
    Active BIT NOT NULL
    )
    GO
    INSERT INTO Students
    VALUES
    ('Jack','Roberts','2/15/1984', '7/7/2006','Computer Science',1)
    INSERT INTO Students
    VALUES
    ('Mary','Paul','5/19/1984', '7/7/2006','Information Systems',1)
    INSERT INTO Students
    VALUES
    ('Mark','David','8/6/1984', '7/7/2006','Physics',0)
    INSERT INTO Students
    VALUES
    ('Julia','Anderson','3/27/1983', '7/10/2006','Computer
    Science',1)
    GO

    CREATE PROCEDURE GetStudent
    @StudentID INT
    AS
    SELECT StudentID, FirstName, LastName, DateOfBirth,
    AdmissionDate, Major, Active
    FROM Students
    WHERE
    StudentID = @StudentID
    GO
    CREATE PROCEDURE GetAllStudentsIDs
    AS
    SELECT StudentID
    FROM Students

    GO
    CREATE PROCEDURE InsertStudent
    @StudentID INT OUTPUT,
    @FirstName NVARCHAR(20),
    @LastName NVARCHAR(20),
    @DateOfBirth DATETIME,
    @AdmissionDate DATETIME,
    @Major NVARCHAR(40),
    @Active BIT
    AS
    INSERT INTO Students
    (FirstName, LastName, DateOfBirth, AdmissionDate, Major, Active)
    VALUES
    (@FirstName, @LastName, @DateOfBirth, @AdmissionDate, @Major,
    @Active)
    SET @StudentID = SCOPE_IDENTITY()
    GO

    CREATE PROCEDURE DeleteStudent
    @StudentID INT
    AS
    DELETE FROM Students
    WHERE StudentID = @StudentID
    GO

    CREATE PROCEDURE UpdateStudent
    @StudentID INT,
    @FirstName NVARCHAR(20),
    @LastName NVARCHAR(20),
    @DateOfBirth DATETIME,
    @AdmissionDate DATETIME,
    @Major NVARCHAR(40),
    @Active BIT
    AS
    UPDATE Students
    SET FirstName = @FirstName,
    LastName = @LastName,
    DateOfBirth = @DateOfBirth,
    AdmissionDate = @AdmissionDate,
    Major = @Major,
    Active = @Active
    WHERE StudentID = @StudentID
    GO

    The T-SQL code written above creates five stored procedures for executing SELECT, INSERT, UPDATE and DELETE operations after creating the database and its Students table. The Students table has seven columns; the first is the primary key and also an IDENTITY column. Let's move on to the StudentDataAccess and see what we want to accomplish with it.

    More ASP.NET Articles
    More By Michael Youssef


       · The StudentDataAccess class has the very same functionality of the StudentDB class,...
       · it's awesome article anyway thank you so much i'm sure you are very smart man,...
       · Thank you for your comment. I always like my readers to comment about the articles ,...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek