Click here to Skip to main content
15,850,921 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
I want to pass null value for datetime object. i try DateTime? dt= null.
so, here also i have to write DateTime? in method which i don't want. so i try another i.e. i assign DBNull.Value to datetime object. but it gives an error that 'Cannot implicitly convert type 'System.DBNull to System.DateTime'.
so what can i do?
Posted
Comments
[no name] 24-Apr-14 7:17am    
Declare the Datetime variable as nullable
[no name] 24-Apr-14 7:17am    
Use\http://www.dotnetperls.com/nullable-datetime

If Allow Nulls is checked for the field. I think the following links will help you:
http://www.c-sharpcorner.com/UploadFile/sd_patel/EnterNullValuesForDateTime11222005015742AM/EnterNullValuesForDateTime.aspx[^]

http://social.msdn.microsoft.com/Forums/en-US/devdocs/thread/d5a42234-0b9e-4a8e-9bfa-354869e74802/[^]

For further information please navigate the following links:

http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx[^]
http://msdn.microsoft.com/en-us/library/system.dbnull.aspx[^]

If i misunderstand your question, please feel free to correct me.
I hope the above information will be helpful. If you have more concerns, please let me know.
 
Share this answer
 
v2
DateTime datatype shouldn't be null. Instead use DateTime.Minvalue which stores the minimum value.
 
Share this answer
 
Comments
Monjurul Habib 5-Mar-11 6:14am    
can you please clarify why "DateTime datatype shouldn't be null."??
m@dhu 5-Mar-11 7:38am    
I should be more specific Cannot implicitly convert type 'System.DBNull to System.DateTime The error raises when inserting null to datetime(not null). In that sense it shouldn't be null, some value to be inserted.
A value type such as System.DataTime cannot accept null or DBNull value. This type should also have a value.

There is a popular method of using DateTime.MinValue. This is nothing like yes another programming fraud. MinValue represents the time point of 00:00:00.0000000, January 1, 0001. Why on Earth this time should be considered as invalid (while a time of one millisecond later is valid)?!

I think there is only one decent solution: use a nullable type derived from DataTime.

C#
System.DataTime? MyDateTime;

//MyDateTime can be assigned to null or vSystem.DateTime value:
MyDateTime = null; //if database returns DBNull
//...
MyDateTime = DateTime.Now; //or any valid valid System.DateTime value


For more information on nullable types, see http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^].
A primary purpose of introduction of nullable types was handling of the situations similar to the one described in the present Question.
—SA
 
Share this answer
 
v2
C#
string mindate = "01/01/1900";
DateTime? value = Convert.ToDateTime(mindate);
cmd.Parameters.AddWithValue("@start_date", string.IsNullOrEmpty(start_date) ? value  : Convert.ToDateTime(start_date));


if DateTime? value=null; using
value= null means it gets default 
That is value= 1/1/0001 12:00:00 AM 
but SqlDateTime Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

so SqlDateTime overflow occurs

so that DateTime? value=null not working

But If you Using DateTime2 to design table 
it accept value= 1/1/0001 12:00:00 AM 
 
Share this answer
 
v4
Comments
CHill60 25-Feb-15 5:14am    
Question was asked 4 years ago and adequately answered. You do make a valid point about using DateTime2 instead, but the point is lost in the way you have worded and formatted this response
You can use something like.
<code>
DateTime date = dbval == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dbval);
</code>
 
Share this answer
 
v3
pass Datetime as yourDate.MinDate
 
Share this answer
 
Comments
CHill60 24-Apr-14 7:30am    
I don't think the OP has waited over 3 years for a solution that just repeats an early one
 
Share this answer
 
Comments
CHill60 24-Apr-14 7:30am    
Doubt the OP has waited over 3 years for a solution
You can take nullable date time.

For example
DateTime? dateVariable = null;
 
Share this answer
 
Comments
CHill60 25-Feb-15 5:09am    
As suggested 3 years ago by other posters

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