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

Problems with nullable DateTime

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

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