-1

my code:

private void txtSubTotal_TextChanged(object sender, EventArgs e)
{
     double subTotal = 0;
     subTotal = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtQuantity.Text);
     txtSubTotal.Text = "" + subTotal;
}

the error points out to this part of the code what could be wrong?

my error points at this part of my code what could be the problem?

Gustav
  • 53,498
  • 7
  • 29
  • 55
chari
  • 1
  • 4
  • txtPrice.Text string OR/AND txtQuantity.Text is not a valid string that can be converted to double. – christophe.chapron May 21 '17 at 15:54
  • perhaps could it be because the values in those 2 textboxes are from ms access database?? – chari May 21 '17 at 15:55
  • What is happening is that the input string was not in a correct format. Your problem is that the format of the input string is not correct. – Mike Nakis May 21 '17 at 15:57
  • Possible duplicate of [Input string was not in a correct format](http://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format) – Dave Cousineau May 21 '17 at 20:54

4 Answers4

0

In the case the text typed in the txtPrice or txtQuantity is not a number Convert.ToDouble will fail. So you can try approachs like this:

double subTotal = 0;
double price, quantity;

if (double.TryParse(txtPrice.Text, out price) && 
    double.TryParse(txtQuantity.Text, out quantity))
{
    subTotal = price * quantity;
}
else
{
    //Notify the exception
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
0

You can use MaskedTextBox control to ensure user can input valid data in your text box, by the way you can use the following code instead:

    double price;
    double qty;
    if (Double.TryParse(txtPrice.Text, out price)&&
        Double.TryParse(txtQuantity.Text, out qty)) // if done, both are valid numbers
    {
        double subTotal = 0;
        subTotal = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtQuantity.Text);
        txtSubTotal.Text = "" + subTotal.ToString();
    }
   else{
         MessageBox.Show("Invalid Input");
    }
Ryan
  • 103
  • 5
0
 private void txtSubTotal_TextChanged(object sender, EventArgs e)
  {
   double subTotal = 0;       
   if(string.NullOrEmpty(txtPrice.Text)&&string.NullOrEmpty(txtQuantity.Text)
  {
          subTotal = Convert.ToDouble(txtPrice.Text) * 
          Convert.ToDouble(txtQuantity.Text);
           txtSubTotal.Text = "" + subTotal;
  }
}
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
0

If you are not certain with your input, you should use double.TryParse() method. If the user is not passing expected value then it would throw an error with Convert.ToDouble() method.

But if we are certain that you will pass the only number then you can use Convert.ToDouble()

Convert.ToDouble will throw an exception on non-numbers
Double.Parse will throw an exception on non-numbers or null
Double.TryParse will return false or 0 on any of the above without generating an exception.

Try this

double price, quantity;
if(string.NullOrEmpty(txtPrice.Text)&&string.NullOrEmpty(txtQuantity.Text)
 {
  if (Double.TryParse(txtPrice.Text, out price)&&
    Double.TryParse(txtQuantity.Text, out quantity)) 
    {
         double subTotal = 0;
         subTotal = price * quantity;             
         txtSubTotal.Text = "" + subTotal.ToString();
    }

 }