Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim()) * Convert.ToInt16(txtQuantity.Text.Trim());

What I have tried:

decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim()) * Convert.ToInt16(txtQuantity.Text.Trim());
Posted
Updated 5-Mar-18 20:21pm
Comments
Karthik_Mahalingam 6-Mar-18 2:15am    
What is the error message?

1 solution

Simple: don't use Convert on user input.
Instead, use the appropriate TryParse methods:
C#
decimal discountedCashPrice, quantity;
if (!decimal.TryParse(txtDiscountedCashPrice.Text.Trim(), out discountedCashPrice))
   {
   ... report problem to user
   return;
   }
if (!decimal.TryParse(txtQuantity.Text.Trim(), out quantity))
   {
   ... report problem to user
   return;
   }
decimal DisCp = discountedCashPrice * quantity;
The Convert methods always throw an exception if the user enters something wrong - and they do that, users, all the time - but the TryParse methods don't - they allow you to gracefully tell the user what the problem is, and let him fix it instead of crashing your app.
 
Share this answer
 

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