I've been working through this for a while and I'm missing the obvious. I'm trying to make sure that if a textbox value is left blank an error isn't thrown and instead a simple message is displayed in the textbox. However, what I've got and several other methods I've tried similar to this haven't worked. I can't figure out why this isn't working and what I need to do differently.
Basics:
This is a simple calculator that allows one to enter their waist, neck, and height measurements to use in a formula that calculates their estimated body fat percentage. The calculation works properly unless a field is left blank.
Thanks for any help!
My code:
if (TBWaist.Text == null || TBNeck.Text == null || TBHeight.Text == null)
{
TBBodyFat.Text = "Value missing";
}
else
if (TBWaist.Text != null & TBNeck.Text != null & TBHeight.Text != null)
{
double waist;
double neck;
double height;
waist = Convert.ToDouble(TBWaist.Text);
neck = Convert.ToDouble(TBNeck.Text);
height = Convert.ToDouble(TBHeight.Text);
TBBodyFat.Text = Convert.ToString(String.Format("{0:p2}", ((501.5 / (1.0324 - .19077 * (Math.Log10(waist - neck)) + .15456 * (Math.Log10(height))) - 450) / 100)));
Error Message
This is the error message I get if I leave the waist textbox blank. I get the same error if I leave any of the others blank as well.
Input string was not in a correct format on line 45.
waist = Convert.ToDouble(TBWaist.Text);