Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 Windows SQL-Server-2008 , +
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 at 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
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
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