Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

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

I am trying to initialize a data variable.  I initializing the variable to 0's but when I saved it to the database the database gave an error message of

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Now, I am trying to set the variable to this date and time for the initilization.  When I try to set the variable it gives me the Cannot Implicitly convert type 'string' to SystemDateTime.

Can someone show me the correct syntax to accomplish this task?

Here is my code:
DateTime dt = new DateTime();
 
                    if (dt.CompareTo(new DateTime()) == 0)
                    {
                        // Date is set to min, 1/1/0001 12:00:00 AM
                        // SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
 
                        dt = "1/1/1753 12:00:00 AM";
 
                    }
                    else
                    {
                        MyGlobalVars.dob = Convert.ToDateTime(userrec.patuser.dob);
                    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada 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
A string has to be explicitly converted to datetime... Using either DateTime.Parse or Convert.ToDateTime
Avatar of Munawar Hussain
hi,

using datetime as string literal can be done with # symbol as prefix and postfix

dt = "#1/1/1753 12:00:00 AM#";



-thanks
Avatar of kwh3856

ASKER

That worked PERFECT!!!!
Thanks
Kenny