473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to set DateTime variable to Null in C#?

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 null, and SQL Server doesn't
accept DateTime.MinValue...

What is the solution to deal with these kind of cases? I probably could
create some work around to send some awkword value that SQL accept and then
treat it from there, but to me there should be some other practical solution
for this!??
(btw: my company hasn't updated to 2.0 yet)

Thanks for your response in advance,
Reza
Jun 29 '06 #1
3 7394
Hi,

DateTime is a value type, it cannot represent null (or DBNull)

simple way is just to set it to DateTime.MinValue and when setting values
for stored proc parameters, check for DateTime.MinValue, and when that's the
value send null to the database (set DBNull.Value to be param value)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Reza Solouki" <Re*********@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...
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 null, and SQL Server doesn't
accept DateTime.MinValue...

What is the solution to deal with these kind of cases? I probably could
create some work around to send some awkword value that SQL accept and
then
treat it from there, but to me there should be some other practical
solution
for this!??
(btw: my company hasn't updated to 2.0 yet)

Thanks for your response in advance,
Reza

Jun 29 '06 #2
Reza,

Just replace nulls with something suitable for your code when you read, and
do the opposite when you write. That's what I am doing and I don't think
there is anything more elegant.

Eliyahu

"Reza Solouki" <Re*********@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...
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 null, and SQL Server doesn't
accept DateTime.MinValue...

What is the solution to deal with these kind of cases? I probably could
create some work around to send some awkword value that SQL accept and
then
treat it from there, but to me there should be some other practical
solution
for this!??
(btw: my company hasn't updated to 2.0 yet)

Thanks for your response in advance,
Reza

Jun 29 '06 #3
"Reza Solouki" <Re*********@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...

Prior to v2, I used to use a separate class for this. Basically, it
simulated the nullable datatype functionality built into v2. I.e. instead of
declaring a DateTime variable / object / class (whatever you want to call
it), I'd declare a NullableDateTime variable, which has a Value property, a
HasValue property etc.

I can let you have a copy of it if you like...
(btw: my company hasn't updated to 2.0 yet)


As soon as you upgrade, you'll be able to do this:

DateTime? dtmTest = null;

if (dtmTest.HasValue)
{
// write the value into the database
}
else
{
// write a NULL into the database
}
Jun 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: juli | last post by:
Hello dear Cor or anyone around!:) This didn't help me(convert) : I have 3 strings in an array : str='11/02/04' ,str='11:23:00" and str=AM. How do I convert str+str+str to a proper datetime...
2
by: Fred Nelson | last post by:
Hi: Another C# newby question: How do I declare a null DateTime variable: DateTime myDate = null; I get the error:
2
by: Solution Seeker | last post by:
Hi, I need a solution to assign a Null to a Datetime Variable / Object. Am willing to know whether it is possible? I will appreciate you if any one knows the solution for it . The...
5
by: GG | last post by:
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 dtSearchFromData.Columns.Add( new...
2
by: Bill Gower | last post by:
How do I assign a null value to a datetime variable? DateTime DateNonMember = DateTime.Parse("").ToString(); This doesn't work. I also tried DateTime DateNonMember = DBNull.Value
4
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
5
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.
12
by: GaryDean | last post by:
Is there any good way in which to deal with null datetimes. For instance, I can't seem to find anything that will pass a null value in a datetime parameter to another method. Convert.dbnull will...
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.