asp.net - Getting error "INDEX WAS OUT OF BOUNDS" while retrieving check box for checking values selected from sql to c# -
i have been inserted checked values of check box table splitting them different column, same want retrieve values table checked check box through error
"index out of bounds"
the code regarding below
foreach (datarow recpt in ds.tables[5].rows) { (var = 0; <= chkprdrecipients.items.count-1; i++) { var recipients = recpt["recipientid"].tostring(); array arrrecipients = recipients.split(','); (var j = 0; j <= recipients.length - 1; j++) { if (arrrecipients.getvalue(j).tostring().trim().tolower() == chkprdrecipients.items[i].value.trim().tolower()) { chkprdrecipients.items[i].selected = true; } } } }
please find solution....
the problem you're using length of string upper bound of j
, instead of length of array. you'll rid of immediate error using:
for (int j = 0; j < arrrecipient.length; j++)
however, code still ugly - why using array
instead of string[]
? code simpler way. i'd rename variables follow normal conventions. example:
foreach (datarow recipientrow in ds.tables[5].rows) { // don't need fetch multiple times, or trim them each time. string[] recipients = ((string) recipientrow["recipientid"]) .split(',') .select(x => x.trim()) .toarray(); // it's possible use foreach loop here, // don't know type of chkprdrecipients... (int = 0; < chkprdrecipients.items.count; i++) { var item = chkprdrecipients.items[i]; foreach (var recipient in recipients) { if (recipient.equals(item.value.trim(), stringcomparison.invariantcultureignorecase)) { item.selected = true; break; // no need check rest of recipients } } } }
Comments
Post a Comment