0

I am trying to display tooltips for each checkbox in my checkboxlist. I need the description to come from the database. The description is pretty long though and I am getting a System.IndexOutOfRangeException. How can I fix this?

int i = 0;
foreach (ListItem l in this.agile_factors.Items) {
    while (dr.Read())
    {
        string description = dr["Description"].ToString();
        l.Attributes["title"] = description;
    }
    i++;
}
conn.Close();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aligator3000
  • 335
  • 3
  • 17
  • 2
    what line are you getting the exception on? – Jeff Feb 24 '11 at 22:19
  • where do you get the exception. I have the feeling we miss some essential code... – rene Feb 24 '11 at 22:20
  • Ditto to the 2 comments above. Do you also know that i will always equal agile_factors.Items.Size. – Ash Burlaczenko Feb 24 '11 at 22:21
  • @rene i have same feeling. Variable i is defined/incremented, but never used in posted code. Seems that this variable is causing IOORE. Because code is incomplete, we will never know... – Tomas Voracek Feb 24 '11 at 22:24
  • 1
    And the code doesn't make much sense either. Aren't you missing some assignment to `dr` inside the `foreach` loop? Like this the inner loop will only run on the first iteration of the outer loop. And only the last iteration of the inner loop has any effect. – CodesInChaos Feb 24 '11 at 22:24
  • Your code looks very broken! I think you need a checking line, like if(dr["id"] = l.Attributes["id"]) inside your while loop (before you save the info from the record into the item) and reset your datareader to first record on the first line of the while loop. Otherwise, you are assigning the description field over and over into this one item, then you are at 'end of datareader' by your next item. – rrrhys Feb 24 '11 at 23:01
  • If this question is re-opened please close it as duplicate of http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it – Alexei Levenkov Nov 17 '15 at 00:57

2 Answers2

1

It is as simple as checking for the column's existence before trying to use it:

foreach (ListItem l in this.agile_factors.Items) {
    while (dr.Read())
    {
        if (dr["Description"] != null)
        {
            string description = dr["Description"].ToString();
            l.Attributes["title"] = description;
        }
    }
    i++; 
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chprpipr
  • 2,039
  • 16
  • 17
0

This just isn't going to work.

  1. Your while loop will read through the entire datareader on the first iteration of the foreach loop. l.Attributes["title"] for the first listitem only will be equal to the value from the last record, and then you ought to get an error from reading a datareader past its end on the next foreach iteration. Though I don't think that's "index out of range."

  2. Index out of range could be because the datareader doesn't have a column with one of the names you're using there.

Turning on the debugger ought to clear it up pretty quickly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119