Accepting null fields from a Texbox

Asked

Viewed 276 times

0

When I click on "Save", the program does a check of Texbox with a deal that I did not let pass repeated numbers, the problem is that it error if any Textbox is blank. Below is the Code I made saving the information in an Excel spreadsheet. As I will not always use Texbox, how do I make it pass without accusing error in Texbox blank and send Excel a default value Null?

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);
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

You need to treat the error and make a decision of what to run in that situation. Or even let the error occur, which is the right one. That is, don’t use Convert.ToInt64() because it was made for cases where you are sure the conversion will work. When this is not right you should use TryParse().

Then you must 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 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 put in the Github for future reference.

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

From the point of view of C#, if you really want to null can’t use long, have to use long? that accepts null. I don’t know how Excel treats it. I would do the conversion as shown and create a variable of type long? who will receive the number or null and would place the value there or the null.

Usually you don’t need to use Equals(), can use ==.

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);

Since you didn’t post the rest of the code, there’s no way to optimize the first part of it to know how the array is used.

Browser other questions tagged

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