Hi All
I’ve checked though the forums and seen one or two threads that deal with nullable types and I have a problem with DateTime value types that I need to solve.
I have a business object with various DateTime fields. In effect these are measuring the date and time that certain processes in the application are completed. All are mapped to a Sql Server 2000 database. I am working in .net 2.0 in c#.
The problem lies with the age old problem of the DataTime field in the database allowing null values. The dates will be gathered from the UI and passed to the business object. Where there is a private field and public property declared for the DateTime value type fields there are declared correctly as
private DateTime? dateTimeProperty;
and
public DateTime? DateTimeProperty
{get:set}
and work fine.
when retrieving values from the database and mapping to the fields in the business object I handle them as follows (using a SqlDataReader to loop through the records and instantiate a List<T> of the business objects):
if (reader[“DBFIELD”] == DBNull.Value)
{
businessObject.DateTimeProperty = null;
}
[color=#0000ff]else
[/color]{
businessObject.DateTimeProperty = Convert.ToDateTime(reader[“DBFIELD”]);
}
So, after this preamble, I am encountering a problem when mapping the business object data back to Sql Server. I iterate through the fields and seek to map them to parameters added to a SqlDataSource control which is declared in a data access class.
When I come to set the parameter I have tried:
Parameter pDateTimeField = new Parameter(“DATETIME”, TypeCode.DateTime, “”);
and
Parameter pDateTimeField = new Parameter(“DATETIME”, TypeCode.DateTime, businessObject.DateTimeField.ToString());
and
if (businessObject.DateTimeField == null)
{
Parameter pDateTimeField = new Parameter(“DATETIME”, TypeCode.DateTime, DbNull.Value.ToString());
}
all to no avail.
Can anyone help me solve this???
All the best
Peter Goddard