Balasubramani M
Balasubramani M

Reputation: 8338

Cannot implicitly convert type System.DateTime to System.DateTime? Explicit conversions exist. Are you missing a cast?

I have gone through some questions(link1, link2) regarding this post but still i couldn't the answer and solve the problem.

private void done_Click(object sender, EventArgs e)
    {
        if (datePicker != null)
            Items.Over50Date = datePicker.Value;

        NavigationService.GoBack();
    }

datePicker is the name of DatePicker Tool. Over50Date is likely to be,

public static DateTime Over50Date;

When i assign my datePicker.Value to the Over50Date, i get the error, Cannot convert type System.DateTime to System.DateTime. Where am doing wrong?

Upvotes: 0

Views: 6975

Answers (2)

Complexity
Complexity

Reputation: 5820

Systme.DateTime, while System.DateTime? means a nullable DateTime object, off course, those 2 are not the same.

You should convert your date which yould be null, (pobably the DatePicker) to a DateTime.

[UPDATED]:

Note: It assumes that datePicker.value is a System.DateTime? If not, in which property of the DatePicker is your date stored?

Please try the following code:

private void done_Click(object sender, EventArgs e)
{
    if (datePicker != null)
    {
        if (datePicker.Value != null) 
        {
            Items.Over50Date = Convert.ToDateTime(datePicker.Value);
        }
    }

    NavigationService.GoBack();
}

Upvotes: 1

Mehmet Salih Yildirim
Mehmet Salih Yildirim

Reputation: 101

Try This:

private void done_Click(object sender, EventArgs e)
{
    if (datePicker != null)
        Items.Over50Date = datePicker.Value != null ? datePicker.Value.Value : DateTime.Now;

    NavigationService.GoBack();
}

Upvotes: 2

Related Questions