Accepting null fields in a Texbox

0

When I click on "Save", the program does a check of Texbox with a negotiation that I did not to let pass numbers repeat, the problem is that it of error if any Textbox is blank. Here is the code I saved saving the information in an Excel worksheet. How do I not always use All Texbox , how do I make it pass without acknowledging an error in the% blank% and send a default value Texbox to Excel?

if (!string.IsNullOrEmpty(txtCaixa1.Text) && checarString(txtCaixa1.Text))
{
    valores[0] = Convert.ToInt64(txtCaixa1.Text);
}
if (!string.IsNullOrEmpty(txtCaixa2.Text) && checarString(txtCaixa2.Text))
{
    // usar o link para verificar se existem textBox iguais
    if (!valores.Any(v => v.Equals(Convert.ToInt64(txtCaixa2.Text))))
    {
        valores[1] = Convert.ToInt64(txtCaixa2.Text);
        txtCaixa2.BackColor = Color.White;
    }
    else
    {
        txtCaixa2.BackColor = Color.Yellow;
        messageBoxButtons();
        return;
    }
}
_oleCmd.CommandText = _Consulta;
_oleCmd.Parameters.Add("@MODELOS", OleDbType.VarChar, 50).Value = txtModelo.Text.Trim();
_oleCmd.Parameters.Add("@QUANTIDADE", OleDbType.VarChar, 255).Value = txtTotalCarton.Text.Trim();
_oleCmd.Parameters.Add("@NUMEROCARTON", OleDbType.Integer).Value = Convert.ToInt32(txtCarton.Text);
_oleCmd.Parameters.Add("@SN1", OleDbType.Integer).Value = Convert.ToInt32(txtCaixa1.Text);
_oleCmd.Parameters.Add("@SN2", OleDbType.Integer).Value = Convert.ToInt32(txtCaixa2.Text);
    
asked by anonymous 03.01.2017 / 19:08

2 answers

3

You need to treat the error and make a decision to do in that situation. Or even let the error occur, which is the most correct. That is, do not use Convert.ToInt64() because it was done for cases where you are sure that the conversion will work. When that is not right you should use TryParse() .

Then you should do something like this:

long valor;
if (long.TryParse(txtCaixa1.Text, out valor)) {
    //aqui faz o que deseja se a conversão foi bem sucedida, provavelmente:
    valores[0] = valor;
} else {
    //faça aqui o que deseja, se o valor é inválido, quem sabe:
    valores[0] = 0;
}

In C # 7 you can do:

if (long.TryParse(txtCaixa1.Text, out var valor)) {
    //aqui faz o que deseja se a conversão foi bem sucedida, provavelmente:
    valores[0] = valor;
} else {
    //faça aqui o que deseja, se o valor é inválido, quem sabe:
    valores[0] = 0;
}

I have my doubts if it should be a long number because then it uses int even in the same field, but I left it as it is.

From a C # viewpoint, if you really want to null you can not use long , you have to use long? that accepts null . I do not know how Excel handles this. I would do the conversion as shown and would create a variable of type long? that will get the number or the null and would put the value there or the null .

Normally you do not need to use Equals() , you can use == .

    
03.01.2017 / 19:30
0

The error is in the last 3 lines

oleCmd.CommandText = _Consulta;
    _oleCmd.Parameters.Add("@MODELOS", OleDbType.VarChar, 50).Value = txtModelo.Text.Trim();
    _oleCmd.Parameters.Add("@QUANTIDADE", OleDbType.VarChar, 255).Value = txtTotalCarton.Text.Trim();
// Podem dar erro nessas 3 linhas
    _oleCmd.Parameters.Add("@NUMEROCARTON", OleDbType.Integer).Value = Convert.ToInt32(txtCarton.Text);
    _oleCmd.Parameters.Add("@SN1", OleDbType.Integer).Value = Convert.ToInt32(txtCaixa1.Text);
    _oleCmd.Parameters.Add("@SN2", OleDbType.Integer).Value = Convert.ToInt32(txtCaixa2.Text);

To solve you can implement this function to ensure that the string is a number:

public int TryParseNumber(string val)
    {
        var outPut = 0;
        Int32.TryParse(val, out outPut);
        return outPut;            
    }

And call them in the integer parameters of Parameters.Add

 _oleCmd.Parameters.Add("@NUMEROCARTON", OleDbType.Integer).Value = TryParseNumber(txtCarton.Text);
 _oleCmd.Parameters.Add("@SN1", OleDbType.Integer).Value = TryParseNumber(txtCaixa1.Text);
 _oleCmd.Parameters.Add("@SN2", OleDbType.Integer).Value = TryParseNumber(txtCaixa2.Text);

As you have not posted the rest of the code, you can not optimize the first part of it to know how the array is used.

    
03.01.2017 / 19:51