How to treat an index band exception?

Asked

Viewed 82 times

1

I’m having an exception problem on my show and I’m not sure how to treat it.

Follow the error image in Datagridview.

Erro

private void DGW_solictacao_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewRow row = this.DGW_solictacao.Rows[e.RowIndex];

        this.txt_solicitacao.Text              = row.Cells[0].Value.ToString();
        this.txt_cliente.Text                  = row.Cells[1].Value.ToString();
        this.txt_Contato.Text                  = row.Cells[2].Value.ToString();
        this.txt_solicitante.Text              = row.Cells[3].Value.ToString();
        this.txt_representante.Text            = row.Cells[4].Value.ToString();
        this.txt_atuacao.Text                  = row.Cells[5].Value.ToString();
        this.txt_separador.Text                = row.Cells[7].Value.ToString();
        this.txt_idcliente.Text                = row.Cells[9].Value.ToString();

        string strSQL = @"select 
                    SA.OBS
                    from tbl_SolicitacaoAmostra as SA
                    where SA.Cod_Solicitacao = '" + txt_solicitacao.Text + "'";

        comando = new SqlCommand(strSQL, conm);
        try
        {
            SqlDataAdapter dados = new SqlDataAdapter(comando);
            DataTable dtLista = new DataTable();
            dados.Fill(dtLista);

            if (dtLista.Rows.Count > 0)
            {
                DataRow dr = dtLista.Rows[0];

                txt_observacao.Text = dr[0].ToString();
            }

        }
        catch
        {
            MessageBox.Show("Não existem dados a serem encontrados");
        }
    }
  • 2

    You are accessing an index that does not exist in a collection. Without seeing the code causing the error you cannot help anymore.

  • 1

    You should not treat it, you should correct the programming error in the code. Read comment above.

  • I put the code in the question

1 answer

3


The error is in one of these lines at most in the last, but it may be up to an earlier one:

this.txt_solicitacao.Text              = row.Cells[0].Value.ToString();
this.txt_cliente.Text                  = row.Cells[1].Value.ToString();
this.txt_Contato.Text                  = row.Cells[2].Value.ToString();
this.txt_solicitante.Text              = row.Cells[3].Value.ToString();
this.txt_representante.Text            = row.Cells[4].Value.ToString();
this.txt_atuacao.Text                  = row.Cells[5].Value.ToString();
this.txt_separador.Text                = row.Cells[7].Value.ToString();
this.txt_idcliente.Text                = row.Cells[9].Value.ToString();

I put in the Github for future reference.

Apparently, the last one shouldn’t be nine, but eight, unless you really want to skip a column. But if that’s the case, you need to have 10 columns, if you only have 9 columns, you make a mistake.

The code has security problem.

Browser other questions tagged

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