Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string []k = objP.Salary.Split(',');
            for (int m = 0; m <= k.Length - 1; m++)
            {
                for (int i = 0; i <= chklstSalary.Items.Count; i++)
                {
                    if (chklstSalary.Items[i].Value == k[m])
                    {
                        chklstSalary.Items[m].Selected = true;
                    }
                }
            }




i want to repopulate the checkboxlist which is a comma separate string my code is this

but i am getting an error message

SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

plz help me to solve my problem...
Posted

Try: chklstSalary.Items.Count -1
also, you use i for chklstSalary and cannot mix it with m (when you set the selected). Try this:
C#
if (chklstSalary.Items[i].Value == k[m])
{
   chklstSalary.Items[i].Selected = true;
   break; // no need to loop any further because we already found the matching checkbox
}

Good luck!
 
Share this answer
 
v3
alternatively u can just specify the datasource proprty of checkboxlist as the array containing values
 
Share this answer
 
Looks too complicated for what it's supposed to do.
I would try something like this first:

C#
char[] delimiter = { ',' };
string[] k = objP.Salary.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            foreach (string m in k)
            {
                foreach(item i in chklstSalary)
                {
                   if (i.Value == m)
                    {
                        chklstSalary.i.Selected = true;
                    }
                }
            }
 
Share this answer
 
v2
string lang;

lang = ds.Tables[0].Rows[0]["languages"].ToString();
string[] k = lang.Split(',');
for (int m = 0; m <= k.Length-1 ; m++)
{
for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
{
if (CheckBoxList1 .Items[i].Value == k[m])
{
CheckBoxList1 .Items[m].Selected = true;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900