Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Setup default value if is datetimepicker in sql column null

Exp

What I have tried:

private DateTime emptydate = DateTime.Parse("2018-01-01 00:00:00");



vrijemeodjaveDateTimePicker.Value = string.IsNullOrEmpty(vrijemeodjaveDateTimePicker.Value == emptydate) ? ((object)DBNull.Value : vrijemeodjaveDateTimePicker.Value;
Posted
Updated 24-Mar-18 18:28pm

Based on what described in the original post, assuming dummyDate is the value from the database table column. You can modify the code to check if the column is null, if yes, use the emptyDate else keep the selected date from the DateTimePicker control.

C#
var dummyDate = DBNull.Value; //DateTime.Parse("2018-02-01 12:30:00");  
  
  vrijemeodjaveDateTimePicker.Value =  
      DBNull.Value.Equals(dummyDate) ? emptydate : vrijemeodjaveDateTimePicker.Value;


Or maybe you meant to ask, how to set the DateTimePicker to a default value if the column value is NULL , else use the value from the database column.

C#
vrijemeodjaveDateTimePicker.Value =
    DBNull.Value.Equals(SomeValueFromDatabaseColumn) ? emptydate : SomeValueFromDatabaseColumn;
 
Share this answer
 
v4
Comments
Goran Bibic 25-Mar-18 3:54am    
vrijemeodjaveDateTimePicker.Value =
DBNull.Value.Equals(SomeValueFromDatabaseColumn) ? emptydate : SomeValueFromDatabaseColumn;

This solution is ok...what to use on the end of line?

,,SOme value from database,, ?

vrijemeodjaveDateTimePicker.Value = DBNull.Value.Equals("2018-01-01 00:00:00") ? emptydate : vrijemeodjaveDateTimePicker.Value;

On end of line instead of vrijemeodjaveDateTimePicker.Value;...what to put inside?

"dataGridViewTextBoxColumn2"].Value); somesthing like this?
Goran Bibic 25-Mar-18 3:56am    
Whit this code working fine, but when is null use dummy date...when cell some value I need that value, but whit this code always is dummy date
Maciej Los 25-Mar-18 6:46am    
+5
This will set vrijemeodjaveDateTimePicker.Value to DateTime.Parse("2018-01-01 00:00:00") if vrijemeodjaveDateTimePicker.Value is null:
vrijemeodjaveDateTimePicker.Value ?? DateTime.Parse("2018-01-01 00:00:00");
 
Share this answer
 
v2
Comments
Goran Bibic 24-Mar-18 12:59pm    
vrijemeodjaveDateTimePicker.Value = vrijemeodjaveDateTimePicker.Value ?? DateTime.Parse("2018-01-01 00:00:00");

error is

Severity Code Description Project File Line Suppression State
Error CS0019 Operator '??' cannot be applied to operands of type 'DateTime' and 'DateTime' prijava_radnika C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\prijava_radnika\prijava_radnika\Form1.cs 562 Active
GuyThiebaut 24-Mar-18 13:05pm    
In which case you were incorrect when you stated in your question that the value could be null.

You might want to try this instead:

vrijemeodjaveDateTimePicker.Value = vrijemeodjaveDateTimePicker.Value == System.DateTime.MinValue ? DateTime.Parse("2018-01-01 00:00:00") : vrijemeodjaveDateTimePicker.Value;
Goran Bibic 24-Mar-18 13:16pm    
I try this...but it is not default value...selection changed using first value in datagrid

vrijemeodjaveDateTimePicker.Value = vrijemeodjaveDateTimePicker.Value == System.DateTime.MinValue ? DateTime.Parse("2018-01-01 00:00:00") : vrijemeodjaveDateTimePicker.Value;
GuyThiebaut 24-Mar-18 13:20pm    
I don't understand what you mean.

Please provide a longer question with:
(1)What you tried.
(2)What you expected the result to be.
(3)What the actual result was.
(4)Why the actual result is incorrect.
Goran Bibic 24-Mar-18 13:22pm    
Get the default value in datetimepicker if date value from sql is null...that I want...

default value is
private DateTime emptydate = DateTime.Parse("2018-01-01 00:00:00");

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