I have the code below which works fine, but if I leave the text boxes blank it throws an error. I would like to first check if any are blank then throw an error message in lblSalary.Text. How could I re-write this? C#
Thanks
protected void Button1_Click(object sender, EventArgs e)
{
Double AnnualHours;
AnnualHours = Convert.ToDouble(txtAnnualHours.Text);
Double Rate;
Rate = Convert.ToDouble(txtRate.Text);
Double Salary;
Salary = (AnnualHours * Rate);
lblSalary.Text = "$" + Salary.ToString();
}
}
Instead of
convert.ToDoubleusedouble.TryParseand handle the case where it returns false.NOTE: Because you’re dealing with money, you should use the decimal type, not the double type. Decimal can correctly represent base 10 numbers (123, 157.2, 0.5) exactly, whereas double cannot.
See here: decimal vs double! – Which one should I use and when?