Link to home
Start Free TrialLog in
Avatar of antone405
antone405Flag for Afghanistan

asked on

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'.

I'm completely baffled. I've used this method to convert a datetime to nullable in a bunch of other places with no problems. For some reason it is erroring this time.

It is erroring on the line:  StartDate = StartDate, with the message:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

It is exactly the same as the "EndDate". What am I doing wrong?

Nullable<DateTime> EndDate;
            Nullable<DateTime> StartDate;

            if (txtEndDate.Text.Length == 0)
            {
                EndDate = null;
            }
            else
            {
                EndDate = Convert.ToDateTime(txtEndDate.Text.ToString());
            }

            if (txtStartDate.Text.Length == 0)
            {
                StartDate = null;
            }
            else
            {
                StartDate = Convert.ToDateTime(txtStartDate.Text.ToString());
            }



            NAssessment NAssessment1 = new NAssessment
            {
                ClientID = Convert.ToInt64(Request.QueryString["ClientID"].ToString()),
                Source = Convert.ToByte(ddSource.Text),
                StartDate = StartDate,
                EndDate = EndDate,
                OtherDetail = txtOther.Text.ToString(),
                Valid = true,
                DateStamp = DateTime.Now,
                EnteredBy = Convert.ToByte(usrEnteredBy)
            };

Open in new window

Avatar of p_davis
p_davis

the nullable either needs to be cast to DateTime or you need to use the .Value property to access the datetiem value
ASKER CERTIFIED SOLUTION
Avatar of antone405
antone405
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry but the post that i made answers the question of how to correct the error that you had. you cannot assign a Datetime Variable or even a DateTime? variable a value using a straight DateTime? reference you have to cast it or you have to use the value property.

i don't see how changing the db to allow nulls solves this error.
Avatar of antone405

ASKER

I am no expert. But it did. The code was not changed. Only the DB and I recreated the data mapping for LINQ to SQL classes. I appreciate your help, and I'm sure your solution would be fine, however, it was not the solution I used to fix this issue.
fair enough - i just wanted to make my statement.

i won't object any further.
I see 2 ways:

1. StartDate = StartDate.HasValue ? StartDate.Value : default(DateTime)

2. Put StartDate and EndDate assignments out of initialization list:

if (StartDate.HasValue)
     NAssessment.StartDate = StartDate.Value;

As for why StartDate behaves differently than EndDate I guess it's because it goes first in initialization list. And we just stop at first error.