The index was out of range. It should be non-negative and smaller than the collection size

Asked

Viewed 2,713 times

2

I am creating a program that stores the profiles of all teachers online currently in a list and then displays the first 3 in a label (where a button advances by displaying the next 3 online), but when I will assign the list values in a label it generates the following exception:

System.Argued tofrangeexception: 'The index was out of range. It should be non-negative and smaller than the collection size.'

Program code:

Consultar consultar = new Consultar();
        SqlDataReader dr1 = null;
        SqlDataReader dr2 = null;
        List<string> nome = new List<string>();
        List<byte[]> imagem = new List<byte[]>();
        List<string> disciplina = new List<string>();
        private int i = 0;
        public ProfessoresOn()
        {
            InitializeComponent();
            dr1 = consultar.PesquisaProfOnline();
            while (dr1.Read())
            {
                nome.Add(dr1["NOME_PROFESSOR"].ToString());
                imagem.Add(Encoding.ASCII.GetBytes(dr1["FOTO_PROFESSOR"].ToString()));
                dr2 = consultar.PesquisaDisciplinaProf(dr1["IDPROFESSOR"].ToString());
                while (dr2.Read())
                {
                    disciplina.Add(dr2["NOME_DISCIPLINA"].ToString());
                }
            }
            lblnome1.Content = nome[i];// O erro ocorre nesta linha
            lblnome2.Content = nome[i++];
            lblnome3.Content = nome[i+2];

1 answer

1


This code has at least two problems, one that is not so apparent that is increasing the variable i (in i++), And it doesn’t look like you want this because you’d skip one. But it’s really weird, because this code in the last three lines, the first will always be 0, the second will always be 1 and the third will always be 3 (I think I wanted it to be 2), so this variable doesn’t even make sense.

But the mistake already occurs right away, because the collection nome in this case has zero elements, and so gives the error, so I think it has not entered the loop because it has nothing in the database. Note that for this code would always have to have at least 3 items in the database.

The solution should be to place this execution conditionally. If you have any element you should inform that you have nothing and not try to take the data. For the second and third elements you need to see in your requirement what you should do, error or simply consider it blank or do another action.

From the description it seems that this happens after some has worked (the question does not make it clear that this is a more general tie), and it seems that there is another problem of logic there, but I can not do more because I have nothing in the question that allows it.

And it seems there’s a serious flaw in the use of SqlDataReader in which at least one exception leaves him hanging, but who knows until he is not closing it in normal conditions.

  • ss can see where I went wrong agr. Thank you very much

Browser other questions tagged

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