Empty field check and zero value

1

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

Does anyone have 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;
        {
    
asked by anonymous 04.03.2015 / 17:26

4 answers

1

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

        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
        }
    }
    
04.03.2015 / 17:52
1

Try 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;
    
04.03.2015 / 18:23
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;
 }
    
04.03.2015 / 21:28
0

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

If you want something direcdy do this:

protected void BT_Count_Click (object sender, EventArgs, and)         {         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;
        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 solve. As already posted above by dil_oliveira .

    
04.03.2015 / 18:34