473,587 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.MinVal ue...

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 7408
Hi,

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

simple way is just to set it to DateTime.MinVal ue and when setting values
for stored proc parameters, check for DateTime.MinVal ue, 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*********@di scussions.micro soft.com> wrote in message
news:FB******** *************** ***********@mic rosoft.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.MinVal ue...

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*********@di scussions.micro soft.com> wrote in message
news:FB******** *************** ***********@mic rosoft.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.MinVal ue...

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*********@di scussions.micro soft.com> wrote in message
news:FB******** *************** ***********@mic rosoft.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 NullableDateTim e 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.HasVal ue)
{
// 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
2399
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 variable. Thanks a lot!
2
17704
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
2403
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 Situation is
5
51591
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 DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any...
2
7140
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
10691
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 = DateTime.Parse(oldRow.ToString()); The program is failing on the if saying that the null value in the field in
5
2856
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
2479
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 not work for instance. Getting nulls back and forth between C# and SQL Server is always problematic. -- Regards, Gary Blakely
1
31349
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. I then want to pass the value of this text box as a datetime variable to my stored procedure which inserts or updates the row. As I mentioned...
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.