0

I am working with an sql server database and entity framework (version 6).

Look at this code behind code:

public partial class WebForm1 : System.Web.UI.Page
{
    private bdd1Entities bdd = new bdd1Entities();

    protected void Page_Load(object sender, EventArgs e)
    {
            Repeater1.DataSource = bdd.Personnes.ToList();
            Repeater1.DataBind();
    }
}

And here is the repeater source in aspx page:

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <tr>
            <td><%# DataBinder.Eval(Container.DataItem, "Nom")  %></td>
        </tr>
    </ItemTemplate>
    </asp:Repeater>

It works fine but if I remove the ToList() function call in code behind, I got

this exception: System.NotSupportedException

I do not understand why. I just want to understand thanks

Sampath
  • 63,341
  • 64
  • 307
  • 441
Bob5421
  • 7,757
  • 14
  • 81
  • 175

1 Answers1

0

The Repeater control provides support for data binding to any object that implements the System.Collections.IEnumerable interface (such as a DataView,ArrayList, Hashtable etc), or the IListSource interface. In your case, there might not be any implicit conversation exist from either of aforementioned interface to your 'Personnes' instance. This is why you are required to convert it to List or cast it to source interface type.

S.N
  • 4,910
  • 5
  • 31
  • 51
  • Why EF objects does not implements System.Collections.IEnumerable ? Is there a way to change that ? – Bob5421 Sep 09 '16 at 09:04
  • I presume, it might be returning IQueryable and not IEnumerable. You can check this while pointing cursor to your Personnes. Can you tell me what is the type of 'Personnes' when you place cursor ( while debug) – S.N Sep 09 '16 at 09:07
  • @Nair [IQueryable](https://msdn.microsoft.com/en-us/library/bb351562(v=vs.110).aspx) implements `IEnumerable`. – Andriy Tolstoy Sep 09 '16 at 09:23
  • We don't know what the custom type return and I asked to submit more details. Additionally, If you look at one of EF thread (http://stackoverflow.com/questions/6674649/webforms-data-binding-with-ef-code-first-linq-query-error?noredirect=1&lq=1), similar type of issues are discussed where we expect these to work in general. – S.N Sep 09 '16 at 10:00