Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a code where is getting all from data base

C#
while(reader.Read())
{string nr_zbor = System.Convert.ToString(reader.GetDecimal(cod_zbor));
string aeroport = reader.GetString(nume_aeroport);
string companie = reader.GetString(nume_companie);
list.Add(nr_zbor);
list.Add(companie);
list.Add(aeroport);
}


when I tried to disply info
XML
foreach (string fl in fly)
                {
                    string zbor = fl[0].ToString();
                    string companie = fl[1].ToString();
                    string aeroport = fl[2].ToString();
                    ListViewItem searchlist = new ListViewItem(zbor);
                    searchlist.SubItems.Add(companie);
                    searchlist.SubItems.Add(aeroport);
                    listResFly.Items.Add(searchlist);
                }


it give me this error
index was outside the bounds of the array.
at line
C#
string companie = fl[1].ToString();
string aeroport = fl[2].ToString();

How can fix error
Posted

1 solution

What are you doing here?
C#
string zbor = fl[0].ToString();
string companie = fl[1].ToString();
string aeroport = fl[2].ToString();

f1 is a string element in fly, so it looks like you need to split it on some parameters first, and then call f1[0] etc. f1 should only be a single element as it is now.

You should rather make a class called Fly, with zbor, company and aroport as properties, and add this calss in a list. That way you could loop throug the list and simply call it like:
f1.zbor

etc..
 
Share this answer
 
v2
Comments
Member 10256101 20-Nov-13 5:26am    
How to declare string array size?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900