c# - Getting the second record where the sql query is satisfied -
i trying fill list box task names database priority equal item in list<object>
.
the code below populates list box, error in database have 2 records priority of 1 , finds first record , outputs twice. in attempt fix previous error displayed 2 records twice, added break;
makes show first record satisfies sql query.
i doing because user has option sort in order of priority, priority values , store them in list<object>
, sort them via bubble sort implementation, , code below output them list box in order user wants.
so question is, how can output of records database correctly?
for (int = 0; < list.count; i++) { string sql = "select [task name] tasks priority = " + convert.toint32(list[i].getvalue(0)); using (oledbcommand cmd = new oledbcommand(sql, conn)) { using (oledbdatareader datareader = cmd.executereader()) { list<object[]> tasknamelist = new list<object[]>(); if (datareader.hasrows) //if table isnt empty { while (datareader.read()) //loop end of database { object[] tasks = new object[datareader.fieldcount]; //object array of same length amount of task names in database tasknamelist.add(tasks); (int j = 0; j <= datareader.fieldcount - 1; j++) { tasks[j] = datareader[j]; //fill object array task names } tasklist.items.addrange(tasks); //add list box break; } } } } }
i solved issue putting if statement inside while loop test if list box contained task name before added list box. code below follows:
while (datareader.read()) //loop end of database { if (tasklist.items.contains(datareader[0]) == false) //so doesn't duplicate records in list box satisfy priority value { object[] tasks = new object[datareader.fieldcount]; //object array of same length amount of task names in database tasknamelist.add(tasks); (int j = 0; j <= datareader.fieldcount - 1; j++) { tasks[j] = datareader[j]; //fill object array task names } tasklist.items.addrange(tasks); //add list box } }
Comments
Post a Comment