Question : Datetimepicker

Dear Experts I am back here with same problem. I have a datetimepicker and a global function which returns null if check box is not selected, returns date in dd/mm/yyyy format is check box is selected.

Function 1
public static object GetDateValue(DateTimePicker picker)
{
    if (picker.Checked)
        return picker.Value.ToString("dd/MM/yyyy");
    else
        return DBNull.Value;
}
Problem cannot convert source type 'object' to target type 'system.nullable<system.datetime>

Function 2

public static string GetDateValue(DateTimePicker picker)
{
   if (picker.Checked)
       return picker.Value.ToString("dd/MM/yyyy");
   else
       return null;
}

Problem
cannot convert source type 'string' to target type 'system.nullable<system.datetime>

Function 3

public static string GetDateValue(DateTimePicker picker)
{
   return picker.Checked ? picker.Value.ToString("dd/MM/yyyy") : null;
}

problem as same as in function 2

My saving code is banquethall.Effectivedateto = ValidateUI.GetDateValue(dateeffectivedateto);

where Effectivedateto is datetime field and am using oracle DB

Answer : Datetimepicker

Try:

 return DateTime.Parse(picker.Value.ToShortDateString()); // parenthesis lost :)

or as in the snippet below.

But probably you'll need to format this date to string somewhere else
1:
2:
3:
4:
5:
6:
7:
        public static DateTime? GetDateValue(DateTimePicker picker)
        {
            if (picker.Checked)
                return new DateTime(picker.Value.Year, picker.Value.Month, picker.Value.Day);
            else
                return null;
        }
Random Solutions  
 
programming4us programming4us