I keep getting “Index was outside the bounds of the array.” when I try to add items to a listView.
What am I doing wrong?
Here is my code:
string[] h = getBetweenAll(thepage, "\" target=\"_blank\">", "</a>");
foreach (string s in h)
listViewClickbank.Items.Add(new ListViewItem(""));
foreach (ListViewItem i in listViewClickbank.Items)
{
if (i.SubItems[0].Text == "(view mobile)")
{
i.Remove();
}
}
foreach (ListViewItem i in listViewClickbank.Items)
{
if (i.SubItems[0].Text.Contains("recordTitle"))
{
i.Remove();
}
}
string[] u = getBetweenAll(thepage, "<div class=\"description\">", "</div>");
for (int i = 0; i < h.Length && i < listViewClickbank.Items.Count; i++)
{
listViewClickbank.Items[i].SubItems.Add(u[i]);
}
The error appears on this line:
listViewClickbank.Items[i].SubItems.Add(u[i]);
Note that you are using
h.Length, notu.Lengthas a condition in yourforloop. You are adding elements ofu, nothand most probably,u.Lengthis smaller thanh.Lengthand you get Exception when you try to access u[i]. It should be :