473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Similar topics

0
5088
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
8
19223
by: Manish Jain | last post by:
Platform : ASP.Net/C# void SomeFunction(DateTime date) { string text = date.ToString(); //This crashes if date is null } I am using a construct similar to above. I want to check if the date...
2
1868
by: Fabiano | last post by:
Please, i have an webform, that work with a object that receives a DateTime value. I don't want to save date into it. How can i verify if it's value is null or not? Tks Fabiano
8
38007
by: Gidi | last post by:
hello, i have a textbox that represnts a date and i want to pare it to a DateTime, but sometimes this textBox can be empty string and i want to send it to the DataBase as null or as empty Date, how...
2
2900
by: Diogo Alves - Software Developer | last post by:
Hi, I am writing a class to work with a table that I have... I would like to initialize every var that will be used to nulls... I found a problem doing this to DateTime vars How can I add a...
8
4323
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
6
16246
by: FatboyCanteen | last post by:
When I using dataset to append a null value to the datetime field. It throw a error -> can not convert db.null to system.date Can there is any standard to pass a Null value to the DateTime...
3
7403
by: Reza Solouki | last post by:
Hello, I have a system where it gets its data from a file that is provided periodically. There are cases where many values such as dates are blank. Considering DateTime variable doesn't accept...
5
2845
by: shapper | last post by:
Hello, I defined a DateTime variable: Dim dt As New DateTime How can I check if it is empty? Basically I want to check if it was given to it a DateTime value or not.
0
7153
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...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5677
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,...
0
4743
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...
0
3230
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...
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.