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  
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

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 / 5
    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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - 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

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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