[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-03-25
7
Medium Priority
?
4,465 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:antone405
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 28569892
the nullable either needs to be cast to DateTime or you need to use the .Value property to access the datetiem value
0
 

Accepted Solution

by:
antone405 earned 0 total points
ID: 28582177
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
 
LVL 22

Expert Comment

by:p_davis
ID: 28582785
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:antone405
ID: 28583171
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
 
LVL 22

Expert Comment

by:p_davis
ID: 28583416
fair enough - i just wanted to make my statement.

i won't object any further.
0
 
LVL 3

Expert Comment

by:Mike_Mozhaev
ID: 28588217
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question