How to handle an index range exception?

1

I have an exception problem in my program and I'm not sure how to handle it.

The error image in the DataGridView follows.

privatevoidDGW_solictacao_CellClick(objectsender,DataGridViewCellEventArgse){DataGridViewRowrow=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();stringstrSQL=@"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");
        }
    }
    
asked by anonymous 11.10.2017 / 15:26

1 answer

3

The error is in one of these lines, at most in the last, but can be up to a previous 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();

It looks like the last one should not be 9 but 8, unless you really want to skip a column. But if it has to have 10 columns, if you have only 9, it will give you an error.

The code has a security problem.

    
11.10.2017 / 16:57