473,402 Members | 2,064 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,402 software developers and data experts.

DateTime Null Question

How do I set a date field to null in C#?

HS
Nov 19 '05 #1
3 1032
You can't, as it's a value type. Same as an int. If you're looking for database
null semantics then you need to use an alternate data type that supports
the notion of null, like System.Data.SqlTypes.SqlDateTime.

-Brock
DevelopMentor
http://staff.develop.com/ballen
How do I set a date field to null in C#?

HS


Nov 19 '05 #2
The Date variable type cannot be null. It must have a value.
Most people use Date.MinValue (or Date.MaxValue) to represent a null date in
their code.

Example:
Dim MyDate as Date = Date.MinValue

As for inserting it in the database, the other guys already specified that
DBNull.Value is what you'll want to use.

The better news is that in .NET 2.0 all variable types will be nullable.
Here's more info:
http://www.panopticoncentral.net/arc...6/04/1180.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"hecsan07" <he******@hotmail.com> wrote in message
news:6E**********************************@microsof t.com...
How do I set a date field to null in C#?

HS

Nov 19 '05 #3
Yes, Mr.Steve is 100% correct!!

Tq.
Lakshmi Narayanan.R

"Steve C. Orr [MVP, MCSD]" wrote:
The Date variable type cannot be null. It must have a value.
Most people use Date.MinValue (or Date.MaxValue) to represent a null date in
their code.

Example:
Dim MyDate as Date = Date.MinValue

As for inserting it in the database, the other guys already specified that
DBNull.Value is what you'll want to use.

The better news is that in .NET 2.0 all variable types will be nullable.
Here's more info:
http://www.panopticoncentral.net/arc...6/04/1180.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"hecsan07" <he******@hotmail.com> wrote in message
news:6E**********************************@microsof t.com...
How do I set a date field to null in C#?

HS


Nov 19 '05 #4

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

Similar topics

2
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
8
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
by: nic | last post by:
hey, I always struggle with the following: I have an object obj with a property paydate (type = DateTime) Now, after instantiating the object and do several things, I want too check if my...
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: Rey | last post by:
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...
8
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...
3
by: Daves | last post by:
a get { ... } for public property SelectedValue returns DateTime type to be used as a parameter in a Sql update query but I'd like it to return "empty" if no date has been selected... I cannot use...
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...
4
by: Jose Fernandez | last post by:
Hello. how can i cast a null value from a datareader (System.DBNull) to dateTime I have an object. One of its fields is a DateTime type field. From the database i am getting System.DBNull values...
14
by: shapper | last post by:
Hello, I have two nullable DateTime: begin and end. I need to find the range, in month, between both variables. If for some reason, for example begin is null, it is not possible to calculate...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
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...
0
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
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...

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.