Click here to Skip to main content
12,078,298 members (47,439 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# Windows SQL-Server , +
I have a code where is getting all from data base

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
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
string companie = fl[1].ToString();
string aeroport = fl[2].ToString();
How can fix error
Posted 30-Sep-12 22:38pm
carlici371

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What are you doing here?
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..
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160212.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100