ASP.NET
  Home arrow ASP.NET arrow Page 2 - 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 - Introducing the StudentDataAccess class


    (Page 2 of 4 )

    The StudentDataAccess class represents a student record in the Students table and at the same time is capable of performing database operations like SELECT, INSERT, UPDATE and DELETE on the database record it presents, or on the table's records, as we are going to see soon. The first thing that we need to do is define the private fields and the public properties. Those fields and their associated properties represent a record in the Students table. The code of the StudentDataAccess table, after adding the fields and the properties, looks like this:

    using System;
    using System.Data;

    // additional namespaces needed for this class
    using System.Data.SqlClient;
    using System.Web.Configuration;

    public class StudentDataAccess{
      private int studentId = -1;

      public int StudentId{
        get { return studentId; }
      }
      private string firstName;

      public string FirstName{
        get { return firstName; }
        set { firstName = value; }
      }
      private string lastName;

      public string LastName{
        get { return lastName; }
        set { lastName = value; }
      }
      private DateTime dateOfBirth;

      public DateTime DateOfBirth{
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
      }
      private DateTime admissionDate;

      public DateTime AdmissionDate{
        get { return admissionDate; }
        set { admissionDate = value; }
      }
      private string major;

      public string Major{
        get { return major; }
        set { major = value; }
      }
      private bool active;

      public bool Active{
        get { return active; }
        set { active = value; }
      }
    }

    You need to create a new class file called StudentDataAccess.cs in the App_Code folder and copy, or write, this code into it.

    Note that we have assigned the value -1 to the studentId field. This will become useful when you are inserting a new record, for example; I'll cover more on this later. Also we have defined the StudentId property as read only so you can't assign a value to the studentId field; as you saw in the previous section the StudentID column has been defined as an IDENTITY column so its values are generated by the database. This makes sense because you might break the code if you assigned a value that doesn't exist in the database, and if you call a method like DeleteStudent() or UpdateStudent() they wouldn't work as they should. All this is going to make sense later when you write code to test the class's methods.

    Now before we continue you need to write the connection string to the Web.Config file. Replace the <connectionString /> element with the following:

    <connectionStrings>
      <add name="SchoolConnectionString"
        connectionString="Data Source=(local);
        Initial Catalog=School;Integrated Security=True"
        providerName="System.Data.SqlClient"/>
    </connectionStrings>

    And add the following statement to the class:

    private readonly static string connString =
      WebConfigurationManager.ConnectionStrings
    ["SchoolConnectionString"].ConnectionString;

    Now you can use the private readonly static string variable connString to assign the connection string used by the SqlConnection objects to create connections to the database.

    To retrieve the connection string from the C# code you need the following line of code:

    WebConfigurationManager.ConnectionStrings
    ["SchoolConnectionString"].ConnectionString;

    The WebConfigurationManager class has a property called ConnectionStrings that is used to retrieve the connection string from the configuration file. This property is of type ConnectionStringSettingsCollection so we use the indexer syntax to access its individual objects, which, of type ConnectionStringSettings, we retrieve the connection string itself through the use of the ConnectionStringSettings.ConnectionString property.

    Let's start developing the methods we need for this class. We start by developing the constructor.

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