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?
[no name] 24-Apr-14 7:17am    
Declare the Datetime variable as nullable
[no name] 24-Apr-14 7:17am    

If Allow Nulls is checked for the field. I think the following links will help you:[^][^]

For further information please navigate the following links:[^][^]

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
DateTime datatype shouldn't be null. Instead use DateTime.Minvalue which stores the minimum value.
Share this answer
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.

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[^].
A primary purpose of introduction of nullable types was handling of the situations similar to the one described in the present Question.
Share this answer
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
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.
DateTime date = dbval == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dbval);
Share this answer
pass Datetime as yourDate.MinDate
Share this answer
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
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
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