none
Strange behaviour with LinqDataSource and GridView RRS feed

  • Question

  • Hi,
    I've created a simple ASP.NET app displaying products from advetureworks (just two tables: Products and ProductModels, relation many to one) using linq to sql.
    When I use LinqDataSource and GridView to display Products including ProductsModel.Name using the templatefield:
    <asp:TemplateField HeaderText="Model">
    <ItemTemplate>
    <%#Eval("ProductModel.Name") %>
    </ItemTemplate>
    </asp:TemplateField>
    the column is empty. But when I enable editing or deleting in LinqDataSource, the column displays correct values.
    Is this a correct behaviour or am i doing something wrong?

    Many thanks for your advice.


    Wednesday, March 12, 2008 9:04 PM

Answers

  • That's because by default, Linq to SQL uses lazy loading & but apparently there is a bug in that the LinqDataSource doesn't perform the deferred queries & load the subentities/child objects.

     

    When you're in edit/delete/update mode, it loads everything.

     

    You can add a Select="new ( field1, field2, childTable, blah)" to the LinqDataSource to also force it to load everything you need, but be aware that a Select statement means you won't be able to edit the records. Or you can set EnableUpdate=true, handle ContextCreated and set the context's ObjectTrackingEnabled to true, or set the Load Option.

     

    See http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2469812&SiteID=1 for more information.

     

    Thursday, March 27, 2008 4:52 PM

All replies

  •  

    Linq pluralizes your table. you need to make it like this

     

    <asp:TemplateField HeaderText="Model">

    <ItemTemplate>
    <%#Eval("ProductModels.Name") %>
    </ItemTemplate>

    </asp:TemplateField>

     

    Happy Coding...

    Thursday, March 13, 2008 2:52 AM
  • Hi,
    that doesn't help. Actually now it throws an exception that Product doesn't contain a definition for ProductModels.
    <%#Eval("ProductModel.Name") %> works well when i enable inserting or deleting in LinqDataSource, so the problem is why isn't this working with disabled inserting and deleting.

    Thanks for advice.
    Thursday, March 13, 2008 9:09 AM
  • I have found the same problem.  Works fine if I have at least one of the EnableInsert/Update/Delete options. Otherwise Eval is never invoked or returns nothing.

    Tuesday, March 25, 2008 6:20 PM
  • That's because by default, Linq to SQL uses lazy loading & but apparently there is a bug in that the LinqDataSource doesn't perform the deferred queries & load the subentities/child objects.

     

    When you're in edit/delete/update mode, it loads everything.

     

    You can add a Select="new ( field1, field2, childTable, blah)" to the LinqDataSource to also force it to load everything you need, but be aware that a Select statement means you won't be able to edit the records. Or you can set EnableUpdate=true, handle ContextCreated and set the context's ObjectTrackingEnabled to true, or set the Load Option.

     

    See http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2469812&SiteID=1 for more information.

     

    Thursday, March 27, 2008 4:52 PM
  • I have a somewhat related (perhaps) issue where the EVAL works just fine EXCEPT after you do an EDIT-> UPDATE then the EVAL statement fields are blank UNLESS the gridView also includes the actual FK ID as one of the columns in the gridView.

     

    So if I have EmployeeID as a column and EmployeeName via the EVAL statement to go Employees.EmployeeName with LINQ then after EDIT -> UPDATE the EmployeeName field is fine, but if I remove or set EmployeeID as hidden in the gridView then the EVAL statement is blank for EmployeeName after the update...?

     

    Friday, April 11, 2008 7:33 PM