Empty field and zero value check

Asked

Viewed 6,317 times

1

Below are two examples of my codes, I have a text box weight that the user needs to put some weight, and can not be 0. In the first code below, he does the checking though, he runs the rest of my code where I commented, which was not meant to happen. Since if it’s 0 or blank, it should stop the code. In the second code it works when I use 0, but if it is empty of the error by trying to transform to int since there is nothing in the textbox.

Someone has a solution for this?

CODE 1

 protected void BT_Cadastrar_Click(object sender, EventArgs e)
    {
        if (TB_Peso.Text.Trim().ToString() == "")
            LBL_Peso.Visible = true;
        else
            LBL_Peso.Visible = false;
        {
            int zero = Int32.Parse(TB_Peso.Text);
            if (zero == 0)
                LBL_Peso.Visible = true;


            // meu código continua
        }
     }

CODE 2

 protected void BT_Cadastrar_Click(object sender, EventArgs e)
    {
        int zero = Int32.Parse(TB_Peso.Text);
        if (TB_Peso.Text.Trim().ToString() == "" || zero == 0)
            LBL_Peso.Visible = true;
        else
            LBL_Peso.Visible = false;
        {
  • This opens keys after LBL_Peso.Visible = false; is there really? It wasn’t even to compile with this. Anyway, to fix your error of running the rest of the code, just put all of it inside an Else.

4 answers

1

There was a piece of your code that was outside the keys to the ELSE.

        protected void BT_Cadastrar_Click(object sender, EventArgs e)
        {
        if (!string.IsNullOrEmpty(TB_Peso.Text))
            LBL_Peso.Visible = true;
        else
        {
            LBL_Peso.Visible = false;

                int zero = Int32.Parse(TB_Peso.Text);
                if (zero == 0)
                    LBL_Peso.Visible = true;

            // meu código continua
        }
    }
  • But when I put 0 it continues my code, which should not happen. How do I solve?

1

Try it like this:

int zero = 0;            
int.TryParse(TB_Peso.Text, out zero);
if(String.IsNullOrWhiteSpace(TB_Peso.Text) || zero == 0)
    LBL_Peso.Visible = true;
else
    LBL_Peso.Visible = false;

1

You can use Tryparse to check if what you have in the textbox can be converted to Int

Example:

 //Cria uma variável int
 int number;

 //Pega o retorno, se vier, se vier true e por que pode ser convertido para int
 bool result = Int32.TryParse(TB_Peso.Text, out number); 
 if (result)
 {
    int zero = Int32.Parse(TB_Peso.Text);        
 }
 else
 {
     //Faz alguma coisa ou só retorna
     return;
 }

0

The way your code is it only has one option or does what is in IF or ELSE.

If you want something big Do it like this:

protected void Bt_cadastrar_click(Object Sender, Eventargs and) { if (!string.Isnullorempty(Tb_peso.Text)) Lbl_weight.Visible = true; Else { Lbl_weight.Visible = false;

        int zero = Int32.Parse(TB_Peso.Text);
        if (zero == 0)
            LBL_Peso.Visible = true;
        else {  
            // meu código continua
        } 
    }
}

Maybe you can improve the construction like this:

protected void BT_Cadastrar_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(TB_Peso.Text)) || ( Int32.Parse(TB_Peso.Text) == 0 )
        LBL_Peso.Visible = true;
    else
    {
        LBL_Peso.Visible = false;
        // meu código continua
    }
}

Must resolve. As already posted above by dil_oliveira.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.