Look please below this codes throw me : FormatException was unhandled by user code
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are calling
ToStringon the query rather than a single result. Even though you may expect only one value to match your query, it will not return just a single value. You have to instruct it to do so usingSingle,First,Lastor the variations of these that also return a default value (SingleOrDefault,FirstOrDefault,LastOrDefault).In order to avoid an exception, you could change it to use
Decimal.TryParseor you could change your code to use a default value if the LINQ query returns something that won’t parse properly. I’d recommend the former or a combination.Note that in the following example, I have added the call to SingleOrDefault. This ensures that only one value is parsed, even if no value is found, and that if there are multiple results, we get an exception (i.e. it enforces that we get exactly zero or one result to the query).