468,290 Members | 2,104 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 7177
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by juli | last post: by
2 posts views Thread by Fred Nelson | last post: by
2 posts views Thread by Solution Seeker | last post: by
2 posts views Thread by Bill Gower | last post: by
4 posts views Thread by Bill Gower | last post: by
5 posts views Thread by shapper | last post: by
12 posts views Thread by GaryDean | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.