Creating a StudentDB Class for ASP.NET 2.0 - Writing the Student class
(Page 3 of 5 )
As we have said before, the Student class represents a record of the Students database table. That means we need to simulate the table's fields with C# properties for that class. So we are going to create seven properties to represent the record. Please wait until we write the data access code and I'm sure that you will fully understand this technique, but for now let's see the code for the Student class. The following is the code you need to copy and paste, or write it by hand if you wish, in place of the auto-generated code of the Student.cs file:
using System;
public class Student
{
private int studentId;
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; }
}
public Student(int studentId, string firstName, string lastName, DateTime dateOfBirth, DateTime admissionDate, string major, bool active)
{
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.admissionDate = admissionDate;
this.major = major;
this.active = active;
}
}
As you can see, there is nothing complicated about this class, but note the difference between the data types used here and the data types we used when we created the Students table on SQL Server, like NVARCHAR, INT, DATETIME and BIT. We will talk more about that in the next part of the series, but for now let's continue.
We have also added a constructor to the class that accepts values for all the private instance variables as parameters. You will see how this constructor is useful soon. Now let's do one more thing. Before we move on to creating the StudentDB class, we need to create the connection string that will be used by the static methods of the StudentDB class to access the database. Let's do that now.
Next: Storing the Connection String in a Web.Config file >>
More ASP.NET Articles
More By Michael Youssef