Trying to compare values of a checkbox list with values of an array list c# with asp.net -
we programming student information system. have far checkboxlist contains modules course selected student enrolled on. these values both stored in database. trying have list item values within checkboxlist selected/ticked based upon modules studying, based on stored in database.
here our c# code:
string connectionstring = webconfigurationmanager.connectionstrings["connectionstring"].connectionstring; sqlconnection myconnection = new sqlconnection(connectionstring); myconnection.open(); string com5 = "select moduleid studentmodules studentid = @studentid"; sqlcommand mycommand5 = new sqlcommand(com5, myconnection); studentidlabel.text = searchtext.text; int studentidint = convert.toint32(studentidlabel.text); mycommand5.parameters.addwithvalue("@studentid", studentidint); string comparemodules = mycommand5.executescalar().tostring(); var listofstrings = new list<string>(); sqldatareader reader = mycommand5.executereader(); while (reader.read()) { string currentvalue = reader.tostring(); listofstrings.add(currentvalue); } string[] arrayofstrings = listofstrings.toarray(); foreach (listitem li in this.cboptional.items) { (int x = 0; x < arrayofstrings.length; x++) { if (li.value.tostring() == arrayofstrings[x]) { li.selected = true; } else { li.selected = false; } } }
from can see there seems issue if statement, when comparing values checkbox list values array.
we tested else clause near bottom of above code, "li.selected = true" make sure code running through foreach statement correctly. items within checkbox list displayed ticked. leads believe there wrong if statement inside loop.
any ideas on appreciated. thanks
you dont need run 2 loops. can iterate through array , check if there list item having same value array element on specific index.
try this.
(int x = 0; x < arrayofstrings.length; x++) { listitem listitemtoselect = this.cboptional.items.findbyvalue(arrayofstrings[x]); if (listitemtoselect != null) { listitemtoselect.selected = true; } }
Comments
Post a Comment