468,306 Members | 1,252 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

DateTime.MinValue and nulls

Rey
Howdy all.

My problem deals w/inserting nulls into database (SQL Svr 2K) for the
datetime fields activityDate and followUpDate where nulls are allowed.
From the web form, the user can type in/select the activity or followup

date.
If, for this exercise, the followupdate textfield is empty, then when
the insert method of the class calls the InsertActivityLog stored
procedure, how can I get nulls inserted instead of 1/1/1900 because of
the use of DateTime.MinValue when instantiating the ActivityLog obj.

BTW, even though the activityDate and followUpDate have values of
{1/1/1} before running the stored proc, it will fail w/an overflow msg
that date has be to from 1795 to etc. Once the SqlDateTime.Null is cast
as a DateTime, the activityDate var now has the value of 1/1/1900. I'd
like to be able to have a null entry in the ActivityDate field. Then
again, I've been staring at this problem too long and probably can't
see the solution...

Thanks in advance for suggestions/comments,

Rey
**********************************************

Below is the code snippet for the insert method.
if (activityDate == DateTime.MinValue)
activityDate = (DateTime)SqlDateTime.Null;

cmd.Parameters.Add("@ActivityDate", activityDate);

if (followUpDate == DateTime.MinValue)
followUpDate = (DateTime)SqlDateTime.Null;

cmd.Parameters.Add("@FollowUpDate", followUpDate);
Code snippet for getting values from various txtfields.
if (txtActivityDate.Text != string.Empty)
actLog.activityDate = DateTime.Parse(txtActivityDate.Text.Trim());
else
if (txtActivityDate.Text == string.Empty)
actLog.activityDate = DateTime.MinValue;

Nov 17 '05 #1
2 15558
Hi

You have to check if it's MinValue , then insert a null, the opposite when
loading, then in all your app you treat MinValue as a null :

//Save
if ( ackdate == DateTime.MinValue )
com.Parameters.Add("@ackdate", SqlDbType.DateTime).Value =
System.DBNull.Value;
else
com.Parameters.Add("@ackdate", SqlDbType.DateTime).Value = ackdate;
//Load
ackdate = reader["AcknowledgeBy"]==System.DBNull.Value?DateTime.MinValue:
Convert.ToDateTime( reader["AcknowledgeBy"]);

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Rey" <re********@cox.net> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Howdy all.

My problem deals w/inserting nulls into database (SQL Svr 2K) for the
datetime fields activityDate and followUpDate where nulls are allowed.
From the web form, the user can type in/select the activity or followup

date.
If, for this exercise, the followupdate textfield is empty, then when
the insert method of the class calls the InsertActivityLog stored
procedure, how can I get nulls inserted instead of 1/1/1900 because of
the use of DateTime.MinValue when instantiating the ActivityLog obj.

BTW, even though the activityDate and followUpDate have values of
{1/1/1} before running the stored proc, it will fail w/an overflow msg
that date has be to from 1795 to etc. Once the SqlDateTime.Null is cast
as a DateTime, the activityDate var now has the value of 1/1/1900. I'd
like to be able to have a null entry in the ActivityDate field. Then
again, I've been staring at this problem too long and probably can't
see the solution...

Thanks in advance for suggestions/comments,

Rey
**********************************************

Below is the code snippet for the insert method.
if (activityDate == DateTime.MinValue)
activityDate = (DateTime)SqlDateTime.Null;

cmd.Parameters.Add("@ActivityDate", activityDate);

if (followUpDate == DateTime.MinValue)
followUpDate = (DateTime)SqlDateTime.Null;

cmd.Parameters.Add("@FollowUpDate", followUpDate);
Code snippet for getting values from various txtfields.
if (txtActivityDate.Text != string.Empty)
actLog.activityDate = DateTime.Parse(txtActivityDate.Text.Trim());
else
if (txtActivityDate.Text == string.Empty)
actLog.activityDate = DateTime.MinValue;

Nov 17 '05 #2
Rey
Howdy Ignacio.
Thanks for the info.

Rey

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Manish Jain | last post: by
2 posts views Thread by Fabiano | last post: by
2 posts views Thread by Diogo Alves - Software Developer | last post: by
8 posts views Thread by craigkenisston | last post: by
6 posts views Thread by FatboyCanteen | last post: by
3 posts views Thread by Reza Solouki | last post: by
5 posts views Thread by shapper | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.