this question has answer here:
i try code display names database in loop shows error index outside bounds of array meaning on line name[j++] = convert.tostring(reader["categoryname"]);
code
con.open(); int count =0; int j =0; //cmd = new sqlcommand("select * category"); string[] name = new string[count]; cmd = new sqlcommand("select categoryname category",con); reader = cmd.executereader(); while (reader.read()) { name[j++] = convert.tostring(reader["categoryname"]); } int loc = 37; checkbox[] obj = new checkbox[count]; (int = 0; < count; i++) { obj[i] = new checkbox(); obj[i].location = new system.drawing.point(loc, 50); obj[i].size = new system.drawing.size(80, 17); obj[i].text = name[i]; this.controls.add(obj[i]); loc += 80; } con.close();
any help?
well set count = 0
create array
string[] name = new string[count];
which means array of 0 lenght , won't able assign it. suggest change list
easier
list<string> name = new list<string>(); while (reader.read()) { name.add(convert.tostring(reader["categoryname"])); }
Comments
Post a Comment