By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

Adding a nullable datetime column to a datatable does not work

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

<GGwrote in message news:Ol****************@TK2MSFTNGP02.phx.gbl...
>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

*** Sent via Developersdex http://www.developersdex.com ***

Jul 21 '06 #2

P: n/a

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.

Jul 21 '06 #3

P: n/a
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" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
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.

Jul 22 '06 #4

P: n/a
This is true. I guess that nullable types are truly convenient only
with strongly typed datasets, then?

Dave Sexton wrote:
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" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...

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.
Jul 22 '06 #5

P: n/a
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" <br*******@canada.comwrote in message news:11**********************@m73g2000cwd.googlegr oups.com...
This is true. I guess that nullable types are truly convenient only
with strongly typed datasets, then?

Dave Sexton wrote:
>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" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googleg roups.com...
>
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.

Jul 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.