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.
Next: Creating the Constructor >>
More ASP.NET Articles
More By Michael Youssef