PC Review


Reply
Thread Tools Rate Thread

Adding a nullable datetime column to a datatable does not work

 
 
GG
Guest
Posts: n/a
 
      20th Jul 2006
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 ***
 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      21st Jul 2006
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> wrote in message news:Oln%(E-Mail Removed)...
>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 ***



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      21st Jul 2006

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.

 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      22nd Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      22nd Jul 2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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.
> >


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      22nd Jul 2006
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >
>> > 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.
>> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way comparing Nullable DateTime Alhambra Eidos Kiquenet Microsoft Dot NET 6 10th Feb 2008 05:46 PM
Datatable Datetime column remove zeroes DG Microsoft C# .NET 0 1st Jun 2006 03:05 PM
Datatable Datetime column remove zeroes DG Microsoft ADO .NET 0 1st Jun 2006 03:05 PM
SqlDateTime overflow error inserting a null date into a nullable sql2000 datetime column using ado.net/vb.net, 1.1 framework Chris Microsoft ADO .NET 2 12th Sep 2004 05:02 AM
to make datetime nullable Alex Liang Microsoft ADO .NET 4 17th Dec 2003 06:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:07 AM.