0

My Code:

DateTime? birthDate = DateTime.Parse(filterDictionary.ContainsKey("DOB") ? filterDictionary["DOB"] : string.Empty); 

I am getting Error Message as "String was not recognized as a valid DateTime." How to solve this issue. Thanks.

0

2 Answers 2

2

The problem (at least one of them) is that you can't parse an empty string to a DateTime.

Change your line of code to this to move the parsing only when you find the key, and return null instead of parsing when you don't have it:

DateTime? birthDate = filterDictionary.ContainsKey("DOB") ? DateTime.Parse( filterDictionary["DOB"]) : (DateTime?) null; 

The other problem might be that your dictionary DOB value is actually not possible to convert to a DateTime. If the above code does not work, please edit your question and post the value in filterDictionary["DOB"] when you get this error.

Sign up to request clarification or add additional context in comments.

4 Comments

type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and '<null>' msg is displayed. Thanks.
@SOUser: See my answer - you need to cast one or other of the operands to DateTime?
Thanks Jon, I have updated my answer now to reflect that change.
@ØyvindKnobloch-Bråthen, Updated answer working fine as expected. Thank You so much.
1

Well DateTime.Parse is always going to fail when you present it with an empty string.

It's not clear whether the time that you've seen this has been one where there has been data in the dictionary but it's invalid, or whether there's been no data and it's parsing string.Empty. Also note that DateTime.Parse returns DateTime, not DateTime?. If you want the value to be null if the entry wasn't in the dictionary, I'd actually use:

DateTime? birthDate = null; string dobText; if (filterDictionary.TryGetValue("DOB", out dobText)) { birthDate = DateTime.Parse(dobText); } 

Or perhaps:

string dobText; DateTime? birthDay = filterDictionary.TryGetValue("DOB", out dobText) ? DateTime.Parse(dobText) : (DateTime?) null; 

Note that you need to cast at least one of the second or third operands to null here so the compiler can work out the type of the conditional expression.

You should also consider whether a plain call to DateTime.Parse is appropriate:

  • If you know the specific format you're expecting, call DateTime.ParseExact
  • If this is user input, you should probably be using TryParse or TryParseExact
  • If it's not user input, you should probably be specifying a parsing culture of CultureInfo.InvariantCulture
  • If it's direct user input in a GUI, is there a way you can avoid getting it as text in the first place?

5 Comments

TryGetValue is not populated.It is asking whether you missing namespace. I put "system.Data.Common". Also the issue occurs. Thanks
@SOUser: I'd assumed that filterDictionary was a Dictionary<string, string> - is it not? (As ever, it's hard to work from partial information.)
Thanks so much. I took the updated answer from oyvind as you corrected. It is working fine. Thanks.
@SOUser: Does that mean you've ignored the various other points in my post? Don't just do the first thing that immediately works for the exact data you're using - stop and consider the wider issues.
Sure. Definitely I will try other alternatives. I will not ignore. Thank You so much.