Adding a nullable datetime column to a datatable does not work

  • Thread starter Thread starter GG
  • Start date Start date
G

GG

I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );

Any ideas?

Thanks
 
Hi GG,

You have to just add the column as DateTime. The default for a new column
is to allow null values.

Check for a null value before casting to DateTime:

DateTime? value;

if (row["MyDateTimeColumn"] is DBNull)
value = null;
else
value = (DateTime) row["MyDateTimeColumn"];

- Dave Sexton
 
GG said:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );

I'm no expert, but I believe that ADO.NET doesn't support the notion of
"not-nullable columns". That is to say, the following has always meant
"column can contain DateTime or null":

dtSearchFromData.Columns.Add(new DataColumn("StartDate",
typeof(DateTime)));
dtSearchFromData.Columns.Add(new DataColumn("EndDate",
typeof(DateTime)));

Then when you retrieve values from that table, you can assign them to a
DateTime? nullable type:

DateTime? start = dtSearchFromData.Rows[0]["StartDate"];

So, there's no need to create the original column with a nullable type.
Just supply the non-nullable version of the type. The column's contents
are nullable by default.
 
Hi Bruce,

This code doesn't work unless dtSearchFromData is strong-Typed. The reason
is that the weak DataRow indexer will always return Type object, which
cannot be implicitly cast to Type Nullable<DateTime>.

Instead, you must explicitly cast to DateTime anyway, which means that you
must explicitly check for DBNull, which can't be cast to either DateTime or
Nullable<DateTime>.

- Dave Sexton

Bruce Wood said:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );

I'm no expert, but I believe that ADO.NET doesn't support the notion of
"not-nullable columns". That is to say, the following has always meant
"column can contain DateTime or null":

dtSearchFromData.Columns.Add(new DataColumn("StartDate",
typeof(DateTime)));
dtSearchFromData.Columns.Add(new DataColumn("EndDate",
typeof(DateTime)));

Then when you retrieve values from that table, you can assign them to a
DateTime? nullable type:

DateTime? start = dtSearchFromData.Rows[0]["StartDate"];

So, there's no need to create the original column with a nullable type.
Just supply the non-nullable version of the type. The column's contents
are nullable by default.
 
This is true. I guess that nullable types are truly convenient only
with strongly typed datasets, then?

Dave said:
Hi Bruce,

This code doesn't work unless dtSearchFromData is strong-Typed. The reason
is that the weak DataRow indexer will always return Type object, which
cannot be implicitly cast to Type Nullable<DateTime>.

Instead, you must explicitly cast to DateTime anyway, which means that you
must explicitly check for DBNull, which can't be cast to either DateTime or
Nullable<DateTime>.

- Dave Sexton

Bruce Wood said:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );

I'm no expert, but I believe that ADO.NET doesn't support the notion of
"not-nullable columns". That is to say, the following has always meant
"column can contain DateTime or null":

dtSearchFromData.Columns.Add(new DataColumn("StartDate",
typeof(DateTime)));
dtSearchFromData.Columns.Add(new DataColumn("EndDate",
typeof(DateTime)));

Then when you retrieve values from that table, you can assign them to a
DateTime? nullable type:

DateTime? start = dtSearchFromData.Rows[0]["StartDate"];

So, there's no need to create the original column with a nullable type.
Just supply the non-nullable version of the type. The column's contents
are nullable by default.
 
Hi Bruce,

If you look at the code generated by the DataSet generator for a DateTime column you'll notice that it simply casts the result of
the DataRow's indexer to DateTime and throws an exception if the value cannot be cast. Nullable in this case is not very useful
since the returned value will never be null!

This means that you must handle null values explicitly in code, regardless of whether you are using a strong-Typed DataSet. If
you'd like to use a Nullable Type to encapsulate the value you can do so but you must explicitly assign null to the variable after
checking for null in the DataColumn using the IsNull method (or strong-Typed Is[Column]Null method).

--
Dave Sexton

Bruce Wood said:
This is true. I guess that nullable types are truly convenient only
with strongly typed datasets, then?

Dave said:
Hi Bruce,

This code doesn't work unless dtSearchFromData is strong-Typed. The reason
is that the weak DataRow indexer will always return Type object, which
cannot be implicitly cast to Type Nullable<DateTime>.

Instead, you must explicitly cast to DateTime anyway, which means that you
must explicitly check for DBNull, which can't be cast to either DateTime or
Nullable<DateTime>.

- Dave Sexton

Bruce Wood said:
GG wrote:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );

I'm no expert, but I believe that ADO.NET doesn't support the notion of
"not-nullable columns". That is to say, the following has always meant
"column can contain DateTime or null":

dtSearchFromData.Columns.Add(new DataColumn("StartDate",
typeof(DateTime)));
dtSearchFromData.Columns.Add(new DataColumn("EndDate",
typeof(DateTime)));

Then when you retrieve values from that table, you can assign them to a
DateTime? nullable type:

DateTime? start = dtSearchFromData.Rows[0]["StartDate"];

So, there's no need to create the original column with a nullable type.
Just supply the non-nullable version of the type. The column's contents
are nullable by default.
 
Back
Top