Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:24:22+00:00 2026-06-01T08:24:22+00:00

I am trying to figure out how to bind a nested collection that I

  • 0

I am trying to figure out how to bind a nested collection that I have retrieved using EF into either an asp:ListView or asp:Repeater control for a web form.

Using EF I created the following query where I am selecting a group of agencies and a list of the entities they are sharing with other agencies.

public static ICollection<Agency> GetAllAgencies()
        {
            ICollection<Agency> retAgencies = null;
            try
            {
                using (var context = new InformSecurityEntities(string.Empty))
                {
                   // retAgencies = context.Agencies.OrderBy(a => a.AgencyName).ToList();
                    retAgencies = (from a in context.Agencies
                                  .Include("SecurityDataShares1")
                                  .OrderBy(a => a.AgencyName)
                                   select a).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception thrown in UserFactory.GetAllAgencies() : " + ex.InnerException + ex.Message); 
            }

            return retAgencies;
        }

I’ve been testing this in a console app and it works fine. Within the console I was rendering my results as follows:

private static void showAgencies()
        {
            var results = UserFactory.GetAllAgencies();
            foreach (var item in results)
            {
                Console.WriteLine("Id: {0} || Name: {1}",
                    item.AgencyId,
                    item.AgencyName);
                foreach (var i in item.SecurityDataShares1)
                {
                    Console.WriteLine(i.ReceivingAgency.AgencyName); 
                    Console.WriteLine(convertEntityToText(i.EntityId));
                }

            }
            Console.WriteLine("enter...");
            Console.ReadLine();
        }

Note: SecurityDataShares1 is an ICollection

What I would like to be able to do is take the results and render them into a format similar to below within a webform:

Agency 1
  Entity 1, Entity 2, Entity 3 ...
Agency 2
  Entity 1, Entity 4, Entity 5 ...

Where I am getting hung up is in the console app I could access the nested collection and iterate over it to render out my results. I have tried using a repeater control with a secondary nested repeater and binding the control to the method results.

<asp:Repeater ID="agencyListRepeater" runat="server" OnItemDataBound="mainRepeaterBound">
                <ItemTemplate>
                    <div class="itemsRow">
                    <div class="column-holder">
                    <asp:Label CssClass="mgmtResultText" ID="lbl_AgencyName" runat="server" Text='<%# Eval("AgencyName") %>' /></div>
                    <br />
                    <div class="mgmtIndentDiv">Sharing data with the following agencies:</div>
                      <asp:Repeater id="nestedDataShare" runat="server">
                      <ItemTemplate>
                      <asp:Label runat="server" Text='<%# Eval("SecurityDataShares1.AgencyName") %>' />
                      <asp:Label runat="server" Text='<%# Eval("SecurityDataShares1.EntityId") %>' />
                      </ItemTemplate>

                      </asp:Repeater>
                    </div>
                </ItemTemplate>
            </asp:Repeater>

Code behind

   if (userRole.IsSysAdmin)
   {
    var agencyData = UserFactory.GetAllAgencies();

    if (agencyData != null || agencyData.Count > 0)
    {
      agencyListRepeater.DataSource = agencyData;
      agencyListRepeater.DataBind();
    }
   else
   ...

However this would fail whenever SecurityDataShares1 was null.

Any suggestions or a best approach for this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:24:24+00:00Added an answer on June 1, 2026 at 8:24 am

    I was never able to get the nested reader to render data, while it would compile, zero (0) results were always returned on the nested objects. As a result I looked to an alternate solution. Bear in mind this is more of a work around to the original questionfor rendering the data within the UI.

    Instead of using a data reader I decided to try injecting html directly into the ui using the LiteralControl class. To do this within the aspx page I added a panel control. You could also use a div element with the attribute ruant=”server” but I decided to use the <asp:Panel/> control:

    So within the ASPX file I added the following:

           <asp:Panel ID="agencyList" runat="server" />
    

    Then within my code behind I created a method with two parameters p = PanelName and a = Collection.

    This approach then allowed me to iterate through the collection and write out to the UI the results as html. Remember LiteralControl only allows the creation of html elements or strings that do not require serverside processing. So if you want to add server side controls you will need to use a separate method for the creation of the object Look here for an example

    protected void CreateHtmlResults(System.Web.UI.WebControls.Panel p,  ICollection<Agency> a)
        {
            var results = a;
            foreach (var item in results)
            {
                p.Controls.Add(new LiteralControl("<p><b>" + item.AgencyName + "</b></p>"));
                foreach (var i in item.SecurityDataShares1)
                {
                    p.Controls.Add(new LiteralControl(i.ReceivingAgency.AgencyName + " " + i.EntityId +"<br/>"));
                }
            }
        }
    

    This is then called in a format such as:

    if (userRole.IsSysAdmin)
      {
      //get a list of all agencies
      agencyData = UserFactory.GetAllAgencies();
    
     if (agencyData != null || agencyData.Count > 0)
     {
       CreateHtmlResults(agencyList, agencyData);                           
     }
     else
     {
        //notify UI that no agencies exist
     }
     }
    

    I’d still appreciate any alternative ideas or approaches otherwise I Hope this helps.

    -cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC site and I am trying to figure out separation
I am trying to figure out the best way to bind to an unrelated
I am currently trying to figure out how you can bind multiple fields in
Trying to figure out the best way to set up collection lists for users
Trying to figure out how to write a jquery formula that will sum all
I'm trying to figure out correct way how to bind something like this with
I'm trying to figure out why this is a problem when using jQuery 1.4.2
I'm trying to figure out how to do texture mapping using GLSL version 4.10.
I have been trying to figure this out, but google wasn't turning up any
I'm trying to figure out what I need to change to bind this code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.