how to execute an action when a field is empty

0

I have two related textbox: sub-total and discount, so that if the sub-total is greater than or equal to 500 the discount will be equal 0.10 I have the following code but when the subtotal textbox is empty the string is in the format wrong.

private void txtsubtotal_TextChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(txtsubtotal.Text) >= 500)
        {
            txtdescuento.Text = "0.10";
        }
        else
        {
            if(Convert.ToInt32(txtsubtotal.Text) <= 499)
            {
                txtdescuento.Text = "0";
            }
            else
            {
                if(txtsubtotal.Text=="")
                {
                    txtdescuento.Text = "0";
                }
            }
    
asked by Samuel Ignacio Susana Confesor 10.12.2018 в 04:55
source

1 answer

2
try{
  if (Convert.ToInt32(txtsubtotal.Text) >= 500)
    {
        txtdescuento.Text = "0.10";
    }else{
        txtdescuento.Text = "0";
    }
}catch(Exeption ex){
    txtdescuento.Text = "0";
}

Try this way since leaving the empty text box will cause an exception and when that happens the discount will be equal to 0 :)

    
answered by 10.12.2018 / 05:04
source