Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-07
16
Medium Priority
?
4,926 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:kwh3856
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 10

Expert Comment

by:oxyoo
ID: 23582644
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
 

Author Comment

by:kwh3856
ID: 23582669
oxyoo
I tried

myCRpatient.DECEASED_DATE = DateTime.TryParse(patient.p.deceasedDate);
It then told me no overload for method.
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 23582709
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:oxyoo
ID: 23582875
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
 
LVL 18

Expert Comment

by:DropZone
ID: 23583699
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
 

Author Comment

by:kwh3856
ID: 23587154
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
 

Author Comment

by:kwh3856
ID: 23587224
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
 
LVL 10

Expert Comment

by:oxyoo
ID: 23587357
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
 

Author Comment

by:kwh3856
ID: 23587377
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
 
LVL 10

Expert Comment

by:oxyoo
ID: 23587389
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
 

Author Comment

by:kwh3856
ID: 23587411
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
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 23587468
ToString should be ToString()
0
 
LVL 10

Accepted Solution

by:
oxyoo earned 2000 total points
ID: 23587474
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
 

Author Comment

by:kwh3856
ID: 23587523
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
 

Author Comment

by:kwh3856
ID: 23587528
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
 

Author Closing Comment

by:kwh3856
ID: 31544181
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

571 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