F
Froefel
Hi group
I am creating a web application that uses a simple DAL as an
ObjectDataSource.
To retrieve data from the database, I use a DataReader object from
which I then assign the various fields to properties in an object,
like so:
in the DB, the fields are defined as follows:
ProjectID int NOT NULL
Description varchar(50) NULL
Status int NULL
Active bit
My object looks like this:
public class Project
{
uint ProjectID;
string Description;
int Status;
bool Active;
}
Project p = new Project();
p.ProjectID = (uint) dr["ProjectID"];
p.Description = dr["Description"].ToString();
p.Status = (int) dr["Status"];
p.Active = (bool) dr["Active"];
I am experiencing a significant amount of problems when dealing with
nullable fields.
Because Description and Status are nullable, I have to check for that
prior to assigning it to the properties, otherwise an exception will
occur.
if (dr["Description"].ToString().Length > 0) p.Description =
dr["Description"].ToString();
if (dr["Status"].ToString().Length > 0) p.Status = (int) dr["Status"];
It seems to me that this is overly complex and very inefficient... Is
there a best practice that I'm missing to address these kind of
conversions and checks?
-- Hans
I am creating a web application that uses a simple DAL as an
ObjectDataSource.
To retrieve data from the database, I use a DataReader object from
which I then assign the various fields to properties in an object,
like so:
in the DB, the fields are defined as follows:
ProjectID int NOT NULL
Description varchar(50) NULL
Status int NULL
Active bit
My object looks like this:
public class Project
{
uint ProjectID;
string Description;
int Status;
bool Active;
}
Project p = new Project();
p.ProjectID = (uint) dr["ProjectID"];
p.Description = dr["Description"].ToString();
p.Status = (int) dr["Status"];
p.Active = (bool) dr["Active"];
I am experiencing a significant amount of problems when dealing with
nullable fields.
Because Description and Status are nullable, I have to check for that
prior to assigning it to the properties, otherwise an exception will
occur.
if (dr["Description"].ToString().Length > 0) p.Description =
dr["Description"].ToString();
if (dr["Status"].ToString().Length > 0) p.Status = (int) dr["Status"];
It seems to me that this is overly complex and very inefficient... Is
there a best practice that I'm missing to address these kind of
conversions and checks?
-- Hans