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;
                }

            }
        }
    }

Recommended Answers

All 4 Replies

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);
        }

        }

Glad you got it figured out. If this is solved please mark it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.