Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, can anyone explain me how to assign a null value to a datetime object in
thanks in advance

Dim dt as DateTime = System.DBNull.Value
Share this answer
s.faizaan7 18-Jul-11 3:32am    
no, it gives error as value type of dbnull cannot be converted to date.
The problem is i have a date time picker with a checkbox, whenever the checkbox is unchecked the dtp is disabled. at this point of time i want to assign null value to my dtp
Simon_Whale 18-Jul-11 4:54am    
You can't assign null value to a date you need to use the nullable(of datetime) to make it work e.g

Dim dt as nullable(of datetime) = nothing
Cimak666 12-Feb-19 15:37pm    
great idea ! ! ! Works perfect!
s.faizaan7 19-Jul-11 6:34am    
thank works
Toniyo Jackson 19-Jul-11 6:38am    
You are welcome :)
If you are using .Net 2.0 or greater, you can always use a nullable type.

Declare your field as DateTime? dt;
Then set dt = null;

In VB.NET, use this:
Dim dt As System.Nullable(Of DateTime)
dt = Nothing
Share this answer
Toniyo Jackson 18-Jul-11 3:32am    
He is using VB.Net?
Member 10235047 12-Nov-13 4:17am    
dtpscheDt.Text = Null
Tarun.K.S 18-Jul-11 3:37am    
Toniyo Jackson 18-Jul-11 3:40am    
This will not work. Please see OP comment in my answer
Tarun.K.S 18-Jul-11 3:38am    
5 for the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900