Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation in ASP.NET with Getter & Setter
    primarykey
    data
    text
    <p>I am trying to do a validation when user input is correct, I used a label with black text to display. On the contrary, when user input is incorrect, I display the error message in another label with red text in ASP.NET. It's a 3 tier program. Here is the codes in my presentation layer:</p> <pre><code> protected void btn_submit_Click(object sender, EventArgs e) { ToolsBLL calcBMI = new ToolsBLL(); lbl_result.Text = ""; lbl_invalid.Text = ""; if (calcBMI.Valid == true) { lbl_result.Text = calcBMI.calcBMI(tb_height.Text, tb_weight.Text); } else if (calcBMI.Valid == false) { lbl_invalid.Text = calcBMI.calcBMI(tb_height.Text, tb_weight.Text); } } </code></pre> <p>It will call the getter and setter which named Valid in BusinessLogicLayer to check whether the input is valid. And here is the codes in my BusinessLogicLayer:</p> <pre><code> public static Boolean valid; public Boolean Valid { get { return valid; } set { valid = value; } } public string calcBMI(string sheight, string sweight) { decimal height = 0; decimal weight = 0; string bmiResultDesc = ""; decimal bmi = 0; ToolsBLL.valid = true; if (sheight == "" &amp;&amp; sweight == "") { ToolsBLL.valid = false; bmiResultDesc = "Height/Weight cannot be blank."; } else { try { height = decimal.Parse(sheight); weight = decimal.Parse(sweight); } catch { ToolsBLL.valid = false; bmiResultDesc = "Height/Weight must be in numeric."; } } if (bmiResultDesc == "") { if (height &lt;= 0) { ToolsBLL.valid = false; bmiResultDesc = "Invalid height"; } else if (weight &lt;= 0) { ToolsBLL.valid = false; bmiResultDesc = "Invalid weight"; } else { BMICalculator.BMICalculator bmiCalc = new BMICalculator.BMICalculator(); bmi = bmiCalc.computeBMI(height, weight); if (bmi &lt;= 0) { ToolsBLL.valid = false; bmiResultDesc = "Error in consuming web services."; } else { ToolsBLL.valid = true; bmiResultDesc = "Your BMI is " + bmi.ToString(); } } } return bmiResultDesc; } </code></pre> <p>I used a getter and setter to set for value for boolean. I am not sure if I am doing in the correct way or not because my BMICalculator behaves strange. For example, when I type in correct input, sometimes it shows in black but sometimes it shows in red. I have no idea why is it so.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload