Hey

Im trying to figure out why my code doesnt enter the second foreach loop??
am i missing something?

public void LoadPageAccess(int intRoleID)
    {
        Roles role = new Roles();

        List<CRMDocLib2012Model.GetPageAccess_Result> GetPageAccess = role.GetPageAccess(intRoleID);

        foreach (CRMDocLib2012Model.GetPageAccess_Result lst in GetPageAccess)
        {
            foreach (GridViewRow Item in gvPageAccess.Rows)
            {
                var chkbox = ((CheckBox)(Item.FindControl("cbPageAccess")));

                var pageID = (Label)(Item.FindControl("lblPageID"));

                if (Convert.ToInt32(pageID.Text) == lst.PageID)
                {
                    chkbox.Checked = true;
                }

            }
        }
    }

Edited 3 Years Ago by pritaeas: Moved.

Nearest I can tell is, if either or both those collections in the foreach loops, is empty the code inside the second one won't run.

You can use the Debug class to determine which is empty, like so:

Debug.WriteLine("outter count: " + GetPageAccess.Count);
foreach (CRMDocLib2012Model.GetPageAccess_Result lst in GetPageAccess)
{
    Debug.WriteLine("inner count: " + gvPageAccess.Rows.Count);
    foreach (GridViewRow Item in gvPageAccess.Rows)
    {
        ...
    }
}

Thanks for the reply

i noticed that the count for the first foreach loop was 0 everytime, i didnt have any code to get the role id for that session so that it can get data from the database

Code looks like this now

public void LoadPageAccess(int intRoleID)
    {

        Roles role = new Roles();
        string strRoleID = this.Session["RoleID"].ToString();
        Security PageAccess = new Security();
        List<CRMDocLib2012Model.GetPageAccess_Result> GetPageAccess = role.GetPageAccess(Convert.ToInt32(strRoleID));


        if (strRoleID != "0")
        {
            foreach (CRMDocLib2012Model.GetPageAccess_Result lst in GetPageAccess)
            {
                foreach (GridViewRow Item in gvPageAccess.Rows)
                {
                    var chkbox = ((CheckBox)(Item.FindControl("cbPageAccess")));

                    var pageID = (Label)(Item.FindControl("lblPageID"));

                    if (Convert.ToInt32(pageID.Text) == lst.PageID)
                    {
                        chkbox.Checked = true;
                    }
                }
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Role does not Exist!');", true);
        }

        }
This question has already been answered. Start a new discussion instead.