Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C#
public void calculation()
       {
           try
           {
              double qty = Convert.ToDouble(textBox1.Text);
             double fat = Convert.ToDouble(textBox3.Text);
              double clr = Convert.ToDouble(textBox4.Text);
             double rate = Convert.ToDouble(textBox6.Text);


               if (radioButton1.Checked==true)
               {
                   if (snf / fat > 1.30)
                   {

                       snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                       textBox5.Text = Convert.ToString(snf);
                       snfrate = Math.Round(rate * (60 / 6.5));
                       fatrate = Math.Round(rate * (40 / 8.5));
                       kgfat = Math.Round(((qty * (fat / 100))), 3);
                       textBox7.Text = kgfat.ToString();
                       kgsnf = Math.Round(((qty * (snf / 100))), 3);
                       textBox8.Text = kgsnf.ToString();
                       amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                       textBox10.Text = amt.ToString();


                   }
                   else
                   {
                       //qty = Convert.ToDouble(textBox1.Text);
                       //fat = Convert.ToDouble(textBox3.Text);
                       //clr = Convert.ToDouble(textBox4.Text);
                       //rate = Convert.ToDouble(textBox6.Text);
                       snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                       textBox5.Text = Convert.ToString(snf);
                       snfrate = Math.Round(rate * (52 / 6.5));
                       fatrate = Math.Round(rate * (48 / 9));
                       kgfat = Math.Round(((qty * (fat / 100))), 3);
                       textBox7.Text = kgfat.ToString();
                       kgsnf = Math.Round(((qty * (snf / 100))), 3);
                       textBox8.Text = kgsnf.ToString();
                       amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                       textBox10.Text = amt.ToString();

                   }

               }
               if (radioButton2.Checked)
               {
                   //qty = Convert.ToDouble(textBox1.Text);
                   //fat = Convert.ToDouble(textBox3.Text);
                   //clr = Convert.ToDouble(textBox4.Text);
                   //rate = Convert.ToDouble(textBox6.Text);
                   snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                   textBox5.Text = Convert.ToString(snf);
                   snfrate = Math.Round(rate * (60 / 6.5));
                   fatrate = Math.Round(rate * (40 / 8.5));
                   kgfat = Math.Round(((qty * (fat / 100))), 3);
                   textBox7.Text = kgfat.ToString();
                   kgsnf = Math.Round(((qty * (snf / 100))), 3);
                   textBox8.Text = kgsnf.ToString();
                   amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                   textBox10.Text = amt.ToString();
               }
               if (radioButton3.Checked)
               {
                   //qty = Convert.ToDouble(textBox1.Text);
                   //fat = Convert.ToDouble(textBox3.Text);
                   //clr = Convert.ToDouble(textBox4.Text);
                   //rate = Convert.ToDouble(textBox6.Text);
                   snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                   textBox5.Text = Convert.ToString(snf);
                   snfrate = Math.Round(rate * (52 / 6.5));
                   fatrate = Math.Round(rate * (48 / 9));
                   kgfat = Math.Round(((qty * (fat / 100))), 3);
                   textBox7.Text = kgfat.ToString();
                   kgsnf = Math.Round(((qty * (snf / 100))), 3);
                   textBox8.Text = kgsnf.ToString();
                   amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                   textBox10.Text = amt.ToString();
               }

           }
           catch (Exception ex)
           { MessageBox.Show(ex.Message); }
       }
Posted
Comments
Richard MacCutchan 8-Dec-11 14:09pm    
Where? What? Please do not expect us to guess what's happening.
Sergey Alexandrovich Kryukov 8-Dec-11 14:20pm    
I can see where :-) -- please see my solution.
--SA
fjdiewornncalwe 8-Dec-11 14:12pm    
You need to run this code through the debugger and step through it line by line. Show us which line is failing so that we can help you. There is no way we can help you if you just dump the entire code block as you have.
Sergey Alexandrovich Kryukov 8-Dec-11 14:13pm    
Exactly. By the exception is in ToDouble -- please see my solution.
--SA
prakash00060 8-Dec-11 14:20pm    
double qty = Convert.ToDouble(textBox1.Text);

this line

Please see the comments to the question. The exception is quite expectable: it happens in ToDouble. You cannot generally avoid it. Catch the exception and instruct to the user how to fix the input in the text boxes to make it correct.

Fix your coding style. Never name controls like that. The names were auto-generated but it does not mean you can leave them as is; this violate Microsoft naming conventions. Rename all such names to give some semantic names to everything. What do you think Visual Studio re-factoring engine is created for?

—SA
 
Share this answer
 
Comments
Monjurul Habib 8-Dec-11 15:31pm    
nice detail,5!
Sergey Alexandrovich Kryukov 8-Dec-11 16:07pm    
Thank you, Monjurul.
--SA
Because you should NEVER use Convert.ToDouble on a user-specified value coming from a TextBox. Use Double.TryParse() instead:

C#
double value1;
if (Double.TryParse(textBox1.Text, out value1))
{
    // it's a valid double, so go ahead and do something with it
}



Beyond that, if I were you, I'd use a decimal type instead of a double because you get MUCH better and predictable precision when you're doing math with a decimal value.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 14:29pm    
I would never use Convert just because the name is misleading? However your never is the exaggeration. Using exception-throwing API (better be Parse, not Convert) can be more convenient then TryParse. Exception is designed for good reasons.

Unfortunately, there is a religious war over using of exceptions for non-errors, but I think this is quite fine. I would not consider giving up convenience and reliability (yes, reliability) of exceptions because of religious arguments. In this case, exception is better as Parse is done several times but exception is caught once. Better to performance as well.

I voted 4.
--SA
#realJSOP 8-Dec-11 14:39pm    
I think excpetions should be reserved for conditions that reflect errant coding, not user input.
Sravan S 9-Dec-11 0:19am    
tryparse is a better option than convert.todouble()
my textbox was empty.without filling the textbox calculation was happing that's why i got this error.
 
Share this answer
 
C#
qty = Convert.ToDouble(textBox1.Text);

this would give an error when the value entered in the text box is non-numeric .Hence go for tryparse so that u cal know whether the dat entered in text box is valid.
 
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