ASP.NET
  Home arrow ASP.NET arrow Creating a StudentDB Class for ASP.NET 2.0
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

Creating a StudentDB Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2007-09-05

    Table of Contents:
  • Creating a StudentDB Class for ASP.NET 2.0
  • Creating the Website and the App_Code Folder
  • Writing the Student class
  • Storing the Connection String in a Web.Config file
  • The StudentDB class

  • 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


    Creating a StudentDB Class for ASP.NET 2.0


    (Page 1 of 5 )

    In this article, and the next two, we are going to talk about how you can create a class called StudentDB and another class called Student. We will be using these classes with a database table called Students and T-SQL stored procedures to execute SELECT, INSERT, UPDATE and DELETE statements, through stored procedures, from our web page.

    What you are going to learn is how you can separate your data access code from the ASP.NET pages so your code will look elegant and easier to debug, manage and modify as well. We will also discuss how you can use generics, one of the new great features of C# 2.0, to return a strongly typed collection that manipulates objects of type Student, and how nice that is for our website.

    We will also see the different syntax involved in creating SqlParameter objects, using the Command.Behavior enumeration to close the connection and to return a single row result set. But today, we are going to create the database table and the stored procedures involved with creating the website and creating the Student class. Then we are going to talk about what we need to do after that.

    Let's start by creating the database and the database table we need for this article and the next two; then we'll insert some data and start working on our website. Run the following code in Microsoft SQL Server Management Studio to create the necessary database, table and INSERT statements that insert a few rows into the Student table.

    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

    You will get the following displayed in the Messages panel:

    We have simply created a database called School, and then created a table inside it called Students. The Students table has an identity and primary key column called StudentID, followed by the first name, last name, date of birth and admission date and finally a column of type BIT to determine whether the student has registered for this semester or not. The SQL Server BIT data type is an integer data type but with restricted values of 0, 1 or NULL.

    Note that our table design doesn't make sense and the database should contain other tables to correctly store and manipulate the data. Still, for the sake of simplicity we are going to work on only one table to concentrate on learning the concepts and the technique discussed in this article and the two articles to follow. After creating the objects we have to insert a few records so we can test our website after writing the code. We have used the T-SQL INSERT statement to do that but you can use Management Studio visual tools to do it without writing any T-SQL code. Now let's move on to the ASP.NET website and see what we need to do.

    More ASP.NET Articles
    More By Michael Youssef


       · i haven't developed any real projects before because i still learning C# and asp.net...
       · Hi, Of course, you can read the article. It's not that difficult if you have...
       · A very good explanation, I visited ASP.net for first time and i find the very first...
       · Thanks, the next 2 article will be interesting more than this one. You will...
       · Very good article and can't wait for the second and third parts. are you going to...
       · Very good article and can't wait for the second and third parts. are you going to...
       · You will like the following articles even more.About the SqlDataSource control, I...
       · Nice article Michael but I need the other articles to complete the class. when we...
       · You will be reading the articles in the next few weeks. Tell me what do you think...
       · If you have an idea about an article related to ASP.NET 2.0 Data Access, please send...
       · Don't get me wrong the article is interesting. However, since it is broken in...
     

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek