Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all

please tell where i am wrong

here is code
obenitiypersonal.reference_date = DateTime.Parse(Txt_reference_date.Text != "" ? Txt_reference_date.Text : (DBNull.Value.ToString ()));

error:
String was not recognized as a valid DateTime.
Posted

Hello

Try this:

C#
obenitiypersonal.reference_date 
                    = DateTime.TryParse(Txt_reference_date.Text
                                 , out obenitiypersonal.reference_date) ?
                                obenitiypersonal.reference_date: new DateTime();
 
Share this answer
 
It seems like Txt_reference_date.Text is not recognized as a valid DateTime. Conditional operator is not the issue here.

You can try :

C#
DateTime d;
obenitiypersonal.reference_date =
   (DateTime.TryParse(Txt_reference_date.Text, out d))
   ? d
   : DateTime.MinValue;


Here I assume that obenitiypersonal.reference_date is a DateTime struct.

If obenitiypersonal.reference_date were a DateTime? (Nullable<datetime></datetime>), you could write :

C#
DateTime d;
obenitiypersonal.reference_date =
   (DateTime.TryParse(Txt_reference_date.Text, out d))
   ? d
   : null;


Basically you cannot get a valid DateTime value out of DBNull.Value, like you do.
 
Share this answer
 
Comments
Shahin Khorshidnia 21-Apr-12 18:29pm    
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'NULL'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900