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

antone405Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
the nullable either needs to be cast to DateTime or you need to use the .Value property to access the datetiem value
0
antone405Author Commented:
Thank you, p_davis. While this wasn't exactly the fix, it did spur me on to check a few other things. The problem ended up being with the fact that I had the StartDate column in the database set to not allow Nulls. The client decided to allow that to be blank after the initial db design. I'm using LINQ, so I changed the table appropriately and it is now operating as intended. So it was a simple oversight on my part by not changing the most important aspect, the database itself!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
p_davisCommented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

antone405Author Commented:
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.
0
p_davisCommented:
fair enough - i just wanted to make my statement.

i won't object any further.
0
Mike_MozhaevCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.