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

Is there a simple way to convert a data field that has be defined as a string into an actual datetime?
myCRpatient.DECEASED_DATE = patient.p.deceasedDate;---------squiggly under patient.p.deceasedDate

Open in new window

kwh3856Asked:
Who is Participating?
 
oxyooCommented:
I'm sorry I was a bit quick. TryParse can not be used with nullables. Here is an updated example:
DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (tmpDateTime.HasValue) {
  DateTime tmpDateTime2 = tmpDateTime.Value;
  
  if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime2)) {
    // success, valid date format
  } else {
    // failed, invalid date format
  }
} else {
  // failed, DateTime is null
}

Open in new window

0
 
oxyooCommented:
DateTime.Parse & DateTime.TryParse lets you convert a String into a DateTime object.
More information about those methods here...
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx


Good Luck!
0
 
kwh3856Author Commented:
oxyoo
I tried

myCRpatient.DECEASED_DATE = DateTime.TryParse(patient.p.deceasedDate);
It then told me no overload for method.
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.

 
Tony McCreathTechnical SEO ConsultantCommented:
The syntax is as follows
if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

0
 
oxyooCommented:
Either use the syntax above (suggested by Tiggerito) which is more "safe" or use:


myCRpatient.DECEASED_DATE = DateTime.Parse(patient.p.deceasedDate);

Open in new window

0
 
DropZoneCommented:
kwh3856,

I see that you have a few of these posts for multiple data type conversions.  Most (all?) data type classes and structures in .NET have a TryParse() method that accepts a string to convert and a variable on which to store the converted value.  The method returns true if parsing succeeded, or false if it failed.  For example:
    if (Int32.TryParse(myString, out myInt))
    {
        // Success! myInt now contains the numeric value of myString
    }
    else
    {
        // Failed! MyInt is not defined.
    }

The TryParse() method only converts from strings to other types.  For conversions between different types, you can use the Convert class.  It has methods to convert to multiple types, such as:
   Convert.ToString()
   Convert.ToInt32()
   Convert.ToByte()
   Convert.ToDate()
   Convert.ToDouble()
   // etc.

Each method is overloaded to accept most data types.  Here's more information on these methods:
    http://articles.techrepublic.com.com/5100-10878_11-5834604.html

And lastly, if you want to convert anything to a string, this is even easier! Every object has a ToString() method that "stringifies" its value.  For example, to convert an Integer to a string, use:
     myIntegerValue.ToString()
And to convert a DateTime value to a string, use:
     myDateTimeValue.ToString()

     I hope this helps.
     -dZ.
0
 
kwh3856Author Commented:
tiggerito,
When I tried your code I got this error message
A property or indexer may not be passed as an out or ref parameter
 

if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))------ squiggle under myCRpatient.Deceased_date
 
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

0
 
kwh3856Author Commented:
tiggerito,
I tried this and got an invalid arguments error message.

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out myCRpatient.DECEASED_DATE))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

0
 
oxyooCommented:
If DECEASED_DATE is a property you can not use it with the out keyword. Properties are a kind of syntactic sugar methods and methods are not allowed to be used in conjunction with out.
Try this instead:

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

0
 
kwh3856Author Commented:
oxyoo,
When I tried your example I got
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
That occured here

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;  ------ squiggle under myCRpatient.DECEASED_DATE
 
and
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
This occured here

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))---squiggle line from DateTime all the the way to the last parenthesis
 
0
 
oxyooCommented:
Hi,
This means that myCRpatient.DECEASED_DATE is nullable. I have adapted the code below (I made the DateTime nullable), and it should work now:
Read about nullables here:
http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx


DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

0
 
kwh3856Author Commented:
oxyoo,
That fixed the first error message but I am still getting the same second error message.  Any ideas?
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
Thanks
Kenny
 
0
 
Tony McCreathTechnical SEO ConsultantCommented:
ToString should be ToString()
0
 
kwh3856Author Commented:
oxyoo,
Thanks for the update but I still get the error on
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
error occurs on this line

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime2))
I am still trying to digest the information your giving me so I am not sure what to try to fix it.
0
 
kwh3856Author Commented:
Got it... i read your above post.....had to add parens to the tostring command

DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (tmpDateTime.HasValue)
{
DateTime tmpDateTime2 = tmpDateTime.Value;
if (DateTime.TryParse(patient.p.deceasedDate.ToString(), out tmpDateTime2))
{
// success, valid date format
}
else
{
// failed, invalid date format
}
}
else
{
// failed, DateTime is null
}
0
 
kwh3856Author Commented:
Thank you very much for sticking out with me.  I am still trying to digest the code but I just need to chew on for awhile and I will understand what you did.  Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.