c# - loop iteration doesnt work like it should -
everything works fine except labels[i]
. loop writing in htmllabel1 , never in htmllabel2 etc. why iteration doesn't work labels[i]
@ reader.getvalue(i)
fine?
mysqlconnection connection = new mysqlconnection(myconnectionstring); mysqlcommand command = connection.createcommand(); command.commandtext = "select number numbers order rand() limit 2; "; mysqldatareader reader; connection.open(); reader = command.executereader(); while (reader.read()) { htmllabel[] labels = new htmllabel[] { htmllabel1, htmllabel2, htmllabel3 }; (int = 0; < reader.fieldcount; i++) { labels[i].text = reader.getvalue(i).tostring(); console.writeline(reader.getvalue(i).tostring()); } } connection.close();
there 1 field reader.fieldcount return 1 every time. if want loop through rows use following code
mysqlconnection connection = new mysqlconnection(myconnectionstring); mysqlcommand command = connection.createcommand(); command.commandtext = "select number numbers order rand() limit 2; "; mysqldatareader reader; connection.open(); reader = command.executereader(); int i=0; htmllabel[] labels = new htmllabel[] { htmllabel1, htmllabel2, htmllabel3 }; while (reader.read()) { //for (int = 0; < reader.fieldcount; i++) //{ labels[i].text = reader[0].tostring(); console.writeline(reader[0].tostring()); //} +=1; } connection.close();
Comments
Post a Comment