c# - Why it always shows the error index is out of array bounds? -
i'm crafting app in c#, here problem stopped me. database table has 7 columns , takes 7 while running application. in line singlerow[c] = data[0].tostring();
shows me error telling me index out of bounds of array. please suggest. code below.
public string[] rowonly() { string[] singlerow = new string[] { }; conn = new oledbconnection(connstr); conn.open(); cmd = new oledbcommand(query, conn); data = cmd.executereader(); messagebox.show(data.hasrows.tostring()); int = data.fieldcount; while (data.read()) { (int c = 0; c < i; c++) { singlerow[c] = data[0].tostring(); } } conn.close(); return singlerow; }
string[] singlerow = new string[] { };
is creating array of length 0
do this:
string[] singlerow = new string[i];
(you'll have move after line initializes i
)
i.e:
int = data.fieldcount; string[] singlerow = new string[i];
tip: use {}
when want initalize collection:
e.g. new string[]{"abc", "def"};
Comments
Post a Comment