LINQ associations issue in Repeater control

Last post 03-06-2008 11:23 PM by Allen Chen – MSFT. 1 replies.

Sort Posts:

  • LINQ associations issue in Repeater control

    03-05-2008, 2:40 PM
    • Loading...
    • tiby_v8
    • Joined on 03-05-2008, 7:15 PM
    • Posts 1

    I have a DataContext created with the designer, that has some one-to-many associations between tables. When I try to access those entities in an Repeater control that gets data from a LINQ Data Source, I get a NullReferenceException. Here is an example of code:

     

    <asp:Repeater runat="server" DataSourceID="LatestArticlesDataSource">
    <ItemTemplate>
    <li>
    <!-- This shows the title of the article --> <h1><%# ((Article)Container.DataItem).Title %></h1>

    <!-- This throws NullReferenceException --> <h2><%# (((Article)Container.DataItem).Category.Title %></h2>
    </li>
    </ItemTemplate>
    </asp:Repeater>

     

    'Article' is a LINQ entity and it's of form:

      

    public partial class Advice : INotifyPropertyChanging, INotifyPropertyChanged
    {
    ...

    [Association(Name="Category_Article", Storage="_Category", ThisKey="CategoryID", IsForeignKey=true)]
    public Category Category { get { ... } set { ... } }

    ...
    }
     
    The DataContext and the entity-related classes are written corectly, as I've tested directly in C# and LINQ the column associations, and it worked perfectly.

    So, how can I solve this problem?

    Thanks!
     

    Filed under: , ,
  • Re: LINQ associations issue in Repeater control

    03-06-2008, 11:23 PM
    Answer

    Hi:

      It's a tricky problem since LinqDataSource is relevant new for users. Please try something like this. In my sample there're two tables, "Student" and "Class". Student table has a foreign key ClassID that is the pk of Class table.

            public string Func(object o)
            {
                string result = "";

              Class c=  DataBinder.Eval(o, "Class") as Class;
              result += c.ClassName + "<br/>";
              IEnumerable list = DataBinder.Eval(o, "Students") as IEnumerable;

              foreach (Student st in list)
              {
                  result += st.StudentName + "&nbsp;";
              }
                return result;
            }

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                DataSourceID="LinqDataSource1">
                <Columns>
                <asp:TemplateField>
                <ItemTemplate>
                <%#Func(Container.DataItem)%>

    </ItemTemplate></asp:TemplateField></Columns>
            </asp:GridView>


            <asp:LinqDataSource ID="LinqDataSource1" runat="server"
                ContextTypeName="WebApplication5.DataClasses3DataContext" GroupBy="Class"

                Select="new (key as Class, it as Students)" TableName="Students">
            </asp:LinqDataSource>

    Student table:

     [Table(Name="dbo.Student")]
     public partial class Student : INotifyPropertyChanging, INotifyPropertyChanged
     {
      

    ...

     [Association(Name="Class_Student", Storage="_Class", ThisKey="ClassID", IsForeignKey=true)]
      public Class Class
      {

    ...

      }

     

    Class table:

     [Table(Name="dbo.Class")]
     public partial class Class : INotifyPropertyChanging, INotifyPropertyChanged
     {
      

    ...

     [Association(Name="Class_Student", Storage="_Students", OtherKey="ClassID")]
      public EntitySet<Student> Students
      {

    ...

    }

    Regards

     
    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter