I have a class named project with the following data members.
class Project
{
private int Record_Num;
private int GCD_ID;
private string Project_Desc;
private string Proponent_Name;
private string Station_ID;
private string OpCentre;
private string Sector_ID;
private string PLZone;
private string Feeder_ID;
private string DxTx_ID;
private string OpControl_ID;
private string Type_ID;
private string ConnKV_ID;
private string Status_ID;
private double MW;
private string Subject;
private int Ip_Num;
private int H1N_ID;
private int NOMS_Slip_Num;
private DateTime NMS_Updated;
private DateTime Received_Date;
private Nullable<DateTime> Actual_IS_Date;
private string Scheduled_IS_Date;
private string UP_Station_ID;
private string UP_Feeder_ID;
private string HV_Circuit;
}
i query the database and retrieve the values through a data table which assigns the value to the project object like this.
for (int prjIdx = 0; prjIdx < dt.Rows.Count; prjIdx++)
{
newProject = new Project(Convert.ToInt32(dt.Rows[prjIdx]["RecordNum"].ToString()), Convert.ToInt32(dt.Rows[prjIdx]["GCDID"].ToString()),
dt.Rows[prjIdx]["ProjectDesc"].ToString(), dt.Rows[prjIdx]["ProponentName"].ToString(),
dt.Rows[prjIdx]["StationName"].ToString(), dt.Rows[prjIdx]["OpCentre"].ToString(),
dt.Rows[prjIdx]["SectorName"].ToString(), dt.Rows[prjIdx]["PLZone"].ToString(),
dt.Rows[prjIdx]["FeederDesc"].ToString(), dt.Rows[prjIdx]["DxTx"].ToString(),
dt.Rows[prjIdx]["OpControl"].ToString(), dt.Rows[prjIdx]["Type"].ToString(),
dt.Rows[prjIdx]["ConnectionKV"].ToString(), dt.Rows[prjIdx]["Status"].ToString(),
Convert.ToDouble(dt.Rows[prjIdx]["MW"]), dt.Rows[prjIdx]["Subject"].ToString(),
Convert.ToInt32(dt.Rows[prjIdx]["IpNum"]), Convert.ToInt32(dt.Rows[prjIdx]["H1NID"]),
Convert.ToInt32(dt.Rows[prjIdx]["NomsSlipNum"]),Convert.ToDateTime(dt.Rows[prjIdx]["NmsUpdated"]),
Convert.ToDateTime(dt.Rows[prjIdx]["ReceivedDate"]),Convert.ToDateTime(dt.Rows[prjIdx]["ActualIsDate"]),
dt.Rows[prjIdx]["ScheduledIsDate"].ToString(),dt.Rows[prjIdx]["UpStation"].ToString(),dt.Rows[prjIdx]["UpFeeder"].ToString(),
dt.Rows[prjIdx]["HVCircuit"].ToString());
newProject.record_num = Convert.ToInt32(dt.Rows[prjIdx]["RecordNum"]);
projList.Add(newProject);
}
now my problem is, all the date time values retrieved from the database can be null.so if it encounters a null value, it fails to convert it and hence it cannot be assigned in to the object. thus it give me an error
how do i tackle the proble.
should i change the date time variable to string data type. but thats a lame solution. please help//
DateTimeis a value-type, you could use the nullable datetime (DateTime?)MSDN Ref on Nullable Structure
Doing so will require changes in how you retrieve the value. One way to do so, would be to implement a
nullorDBNullcheck, and set the value of nullable datetimes in the Project instanciation using a ternary operator.and use it like this in Project’s instanciation :