473,761 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a nullable datetime column to a datatable does not work

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
dtSearchFromDat a.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable <DateTime>) ) );

Any ideas?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Jul 20 '06 #1
5 51667
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["MyDateTimeColu mn"] is DBNull)
value = null;
else
value = (DateTime) row["MyDateTimeColu mn"];

- Dave Sexton

<GGwrote in message news:Ol******** ********@TK2MSF TNGP02.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
dtSearchFromDat a.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable <DateTime>) ) );

Any ideas?

Thanks

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

Jul 21 '06 #2

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
dtSearchFromDat a.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.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":

dtSearchFromDat a.Columns.Add(n ew DataColumn("Sta rtDate",
typeof(DateTime )));
dtSearchFromDat a.Columns.Add(n ew DataColumn("End Date",
typeof(DateTime )));

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

DateTime? start = dtSearchFromDat a.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
Hi Bruce,

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

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<DateTi me>.

- Dave Sexton

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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
dtSearchFromDa ta.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.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":

dtSearchFromDat a.Columns.Add(n ew DataColumn("Sta rtDate",
typeof(DateTime )));
dtSearchFromDat a.Columns.Add(n ew DataColumn("End Date",
typeof(DateTime )));

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

DateTime? start = dtSearchFromDat a.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
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 dtSearchFromDat a is strong-Typed. The reason
is that the weak DataRow indexer will always return Type object, which
cannot be implicitly cast to Type Nullable<DateTi me>.

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<DateTi me>.

- Dave Sexton

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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
dtSearchFromDat a.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.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":

dtSearchFromDat a.Columns.Add(n ew DataColumn("Sta rtDate",
typeof(DateTime )));
dtSearchFromDat a.Columns.Add(n ew DataColumn("End Date",
typeof(DateTime )));

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

DateTime? start = dtSearchFromDat a.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
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*******@cana da.comwrote in message news:11******** **************@ m73g2000cwd.goo glegroups.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 dtSearchFromDat a is strong-Typed. The reason
is that the weak DataRow indexer will always return Type object, which
cannot be implicitly cast to Type Nullable<DateTi me>.

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<DateT ime>.

- Dave Sexton

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******* *************** @h48g2000cwc.go oglegroups.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
dtSearchFromDa ta.Columns.Add( new DataColumn( "StartDate" ,
typeof( DateTime? ) ) );
dtSearchFromDat a.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":

dtSearchFromDat a.Columns.Add(n ew DataColumn("Sta rtDate",
typeof(DateTime )));
dtSearchFromDat a.Columns.Add(n ew DataColumn("End Date",
typeof(DateTime )));

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

DateTime? start = dtSearchFromDat a.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
6509
by: Bruno Panetta | last post by:
Suppose I have a table with the following columns: Year, SalesInEurope, SalesInAmerica, TotalSales. I want to add a new column called SalesInAsia, say, but I want it to appear before TotalSales. How can this be achieved? Thanks, Bruno
1
2999
by: scj | last post by:
Hi all, I need to determine the exact type of DateTime column(*) in an Access 97 database. I'm able to do this with an Access 2 database. With ADO.Net and VB.Net (**), I do something like this : Dim oDT As New DataTable() Dim oCnx As OleDbConnection
1
6798
by: Luis Esteban Valencia | last post by:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0117: 'System.Data.DataTable' does not contain a definition for 'WriteXml' Source Error:
7
1700
by: Patrick Olurotimi Ige | last post by:
I have a simple Stored Procedure with multiple select statements..doing select * from links for example. I created a DataTable and then fill the tables But the first dtTemplate DataTable doesn't give the error but the links does! I get the error on this LINE(when looping):- PageLinks.Text = PageLinks.Text & dtLinks.Rows(iLoop)("link_url")
4
1553
by: Sam | last post by:
Hi, I'm adding columns to a datatable as followed. The values are from textboxes or comboboxes. The first column is properly field but then all the subsequent columns just contain "", whereas the source txtboxes or comboboxes values are correct. Why is that ? thx Dim dt As New DataTable
30
4601
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
2
1330
by: nephish | last post by:
Hey there, i tried this question in the mysql group, but have not heard anything. i have a database with about 20 tables. The some of these tables have datetime type columns in them. What i am basically wondering is if there is a way to make a default value of now() apply if it is not specified when the row is created. i know this is possible with a timestamp, but is it possible with a datetime column ? and if so, how ?
4
7253
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which - "Modified" of type DateTime - is hidden since it should not be edited by a user. The system handles the update for this column. So, I have hidden (Visible=false) this column on the grid. In order to access the value in this field, I have created a...
0
1857
by: premtolani | last post by:
Hello, Can you tell me how to sort a datetime column in any datetime format in a swt table ? the datetime format can be any. regards, Prem
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.