471,066 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Problems with nullable DateTime

Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #1
4 10543
Forget it, I figured it out as I sit here hitting my head for being so
stupid.
"Bill Gower" <bi*******@charter.netwrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field
in the table is not able to be represented as a string. Which is fine, I
can understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #2
On Feb 14, 2:09 pm, "Bill Gower" <billgo...@charter.netwrote:
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.

Bill
The above won't work because: oldRow["datemember"].ToString() will
fail if oldRow["datemember"] is null.

Test for if your datetime field equals to DBNull.Value:

if (oldRow["datemember"] != DBNull.Value)
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
else
DateMember = DateTime.MinValue; //You cannot assign a DateTime
value to null

One liner:
DateMember = oldRow["datemember"] == DBNull.Value ?
DateTime.MinValue :
DateTime.Parse(oldRow["datemember"].ToString());

Have fun.

Quoc Linh

Feb 14 '07 #3
Bill Gower <bi*******@charter.netwrote:
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
Well, that's not your actual code - ToString is a method, not a
property. In future, it would be helpful to post the actual code.
The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
You should examine the value of oldRow["datemember"] *before* trying to
parse it, instead of after.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #4
Mel
to check a field use

oldRow["datemember"] == DBNull.Value

"Bill Gower" <bi*******@charter.netwrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
Why won't this work? What do I need to do to make it work?

DateTime? DateMember;

if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());

The program is failing on the if saying that the null value in the field
in the table is not able to be represented as a string. Which is fine, I
can understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill

Feb 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ESPNSTI | last post: by
9 posts views Thread by Mark Rae | last post: by
reply views Thread by pranesh.nayak | last post: by
15 posts views Thread by scparker | last post: by
5 posts views Thread by =?Utf-8?B?emlubw==?= | last post: by
3 posts views Thread by =?Utf-8?B?V2FubmFiZQ==?= | last post: by
2 posts views Thread by Andrus | last post: by
reply views Thread by leo001 | last post: by

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.