Click here to Skip to main content
15,884,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I am working in C# windows platform where I have to multiply two text box value which is store in a double data type variable below I m give the code

C#
double a=0.00;
//Calculation for Amount
if (txtItemCode.Text != "")
{
     a = float.Parse(txtItemQty.Text);
}
else
{
    double m = double.Parse(txtItemQty.Text);
    double n = double.Parse(txtPrice.Text);

    a = float.Parse(m * n); //here conversion problem started
  //Error: float.Parse(String)has some invalid arguments
}


Please help me to solve the issue

Thanks to All
Indrajit
Posted
Updated 12-Sep-11 19:42pm
v2

As your error explains, parse() expecting a string. Did you read the API?

Did you tried anything such as ,

C#
a = m * n;
 
Share this answer
 
Comments
Venkatesh Mookkan 13-Sep-11 2:26am    
Good answer!
I'm having trouble following your business logic. You start out by declaring a variable a as a double. Then you test the txtItemQty value to see if it is blank, and if it is, you immediately recast the type to float and assign it the value of 0.00. Later you clearly indicate that you mean the variable m to hold the value of txtItemQty as a double; why not be consistent?

Let's do this another way:

C#
if (txtItemQty.Text != "")
   {
      double m = double.Parse(txtItemQty.Text);
      double n = double.Parse(txtPrice.Text);
      double k = m*n;
      MessageBox.Show("Your cost is " & k.ToString());
   }
else
   {
      MessageBox.Show("Please enter a quantity and retry.");
   }
 
Share this answer
 
Comments
Venkatesh Mookkan 13-Sep-11 2:27am    
Good answer!
float.Parse() receive, string as argument pass the variable this way:

C#
double a = 0.00;

          if (txtItemQty.Text != "")
          {
              a = float.Parse(textBox1.Text);
          }
          else
          {
              double m = double.Parse(txtItemQty.Text);
              double n = double.Parse(txtPrice.Text);
              double k = m * n;

              a = float.Parse(k.ToString()); //here conversion problem started
              //Error: float.Parse(String)has some invalid arguments

              MessageBox.Show(a.ToString());
 
Share this answer
 
Comments
Venkatesh Mookkan 13-Sep-11 2:26am    
And why he has to do below?

a = float.Parse(k.ToString());
Sudhir Kumar Srivastava lko 13-Sep-11 7:29am    
float.Parse() takes string as argument so u can not pass a double value or any integer value

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