Foreach loop not looping through checkbox list asp .net

I am trying to read a list of values that exist in a SQL database and if the value exists, it needs to check a checkbox in a checkbox list. At the moment, I can read the values of a specific employee but when I try to loop through the checkbox list, it skips it completely. I can add a new data entry by checking items perfectly but it’s only reading that is causing issues:

My sample code below is to test whether it even loops through the checkbox list.

var asd = GetEmployeeSpecificDisabilities(idNum, connString); //returns a list of a specific employees disabilities;

string value = string.Empty;

foreach (ListItem item in disabilityList.Items)
{
    value += item.Selected ? item.Text + "," : "";

The below code works perfectly when adding new employee details:

//create new list object
EmployeeList newEmployee = new EmployeeList();
    
newEmployee.EmployeeNumber = txtEmployeeNumber.Text;
newEmployee.IDNumber = txtIDnumber.Text;
newEmployee.employeeSurname = txtSurname.Text;
newEmployee.employeeName = txtName.Text;
newEmployee.numberOfDependants = Convert.ToInt32(txtNumOfDependants.Text);
newEmployee.Race = lsRaces.SelectedValue;
newEmployee.Gender = lsGender.SelectedValue;
                    
//if employee has a disability
if (disabilityList.Visible == true)
{
    foreach (ListItem x in disabilityList.Items)
    {
        if (x.Selected)
        {
            newEmployee.Disabilityreference_Id = Convert.ToInt32(x.Value);
            newEmployee.CreateNewEmployee(connString, newEmployee);
        }
    }
} 
else
{
    newEmployee.CreateNewEmployee(connString, newEmployee);
}
    
Response.Redirect("Default.aspx");

Front end code:

<asp:CheckBoxList ID="disabilityList" runat="server" style="left: 19px; top: 3px; width: 266px; border:medium; padding:inherit" DataSourceID="Disabilities" DataTextField="DisabilityReference" DataValueField="Id" Visible ="true" CssClass="checkbox checkbox"  Font-Size="Medium">
        </asp:CheckBoxList>
        <asp:SqlDataSource ID="Disabilities" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeDBConnectionString %>" SelectCommand="SELECT * FROM [Disabilities]"></asp:SqlDataSource>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems you need to iterate GetEmployeeSpecificDisabilities and compare with disabilityList something like that:

var employeeDisabilities = GetEmployeeSpecificDisabilities(idNum, connString);
 
foreach(var employeeDisability in employeeDisabilities)
{
    foreach (ListItem item in disabilityList.Items)
    {
        if(item.Value == employeeDisability.Disabilityreference_Id.ToString())
        {
            item.Selected = true;
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x