GridView databinding & LLBLGenProDataSource2

Posts   
 
    
louieo avatar
louieo
User
Posts: 9
Joined: 24-May-2006
# Posted on: 16-Jun-2006 07:59:12   

Can anyone please help me with this scenario?:

I have LLBLGenPro v2.0 beta generated code (adapter) and I'm using VS.NET 2005. I have successfully managed to get an EntityCollection of "ProductEntity" objects binding to a GridView but a ProductEntity has a ManufacturerEntity object as a property. The ProductEntity also has a ManufacturerId as per the sql table but what I want to show in the GridView is the Name property of that ManufacturerEntity object within the ProductEntity.

Any ideas how I can use a BoundField or TemplateField to drill down to the ManufacturerEntity object and acheive this?

Code below shows how I've attempted to explicitly bind in the GridView, also adding the prefetch for Manufacturer in the ldsProduct_PerformSelect handler.

code is here:

    <cc1:LLBLGenProDataSource2 
        ID="ldsProduct" 
        runat="server" 
        AdapterTypeName="MyApp.DAL.DatabaseSpecific.DataAccessAdapter, MyApp.DALDBSpecific"
        DataContainerType="EntityCollection" 
        EntityFactoryTypeName="MyApp.DAL.FactoryClasses.ProductEntityFactory, MyApp.DAL" 
        OnPerformSelect="ldsProduct_PerformSelect">
    </cc1:LLBLGenProDataSource2>
    
    <asp:GridView 
        ID="grvProductList" runat="server" CssClass="ListGrid" 
        AlternatingRowStyle-CssClass="EvenRow" GridLines="None"
        AllowPaging="True" ShowFooter="True"
        AutoGenerateColumns="False" DataSourceID="ldsProduct" DataKeyNames="Id">
        <EmptyDataTemplate>
            No data found.
        </EmptyDataTemplate>
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:TemplateField>
                <ItemTemplate>
                    <%# ((ManufacturerEntity)((ProductEntity)Container.DataItem).Manufacturer).Name%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CheckBoxField DataField="IsShippable" HeaderText="IsShippable" SortExpression="IsShippable" />
        </Columns>
        <AlternatingRowStyle CssClass="EvenRow" />
    </asp:GridView>

I end up getting an "Object reference not set to an instance of an object" error

Thanks Luke simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 16-Jun-2006 09:19:46   

Please post the stacktrace. The code is big, so it could be anywhere. A stacktrace shows where the exception originated.

Frans Bouma | Lead developer LLBLGen Pro
louieo avatar
louieo
User
Posts: 9
Joined: 24-May-2006
# Posted on: 19-Jun-2006 11:58:44   

here's the stacktrace.....

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.productlist_aspx.__DataBind__control21(Object sender, EventArgs e) +105
   System.Web.UI.Control.OnDataBinding(EventArgs e) +99
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +216
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +216
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +221
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3004
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360
louieo avatar
louieo
User
Posts: 9
Joined: 24-May-2006
# Posted on: 19-Jun-2006 12:09:26   

I just debugged the app and realised that my OnPerformSelect handler is not being called. This is where I am adding a prefetch to fill the Manufacturer object so if it's not being called, that's obviously why I'm getting a null reference error when binding to that object. I've hooked up the handler on the server control as such:

OnPerformSelect="ldsProduct_PerformSelect"

am I doing something stupid Frans?!

louieo avatar
louieo
User
Posts: 9
Joined: 24-May-2006
# Posted on: 19-Jun-2006 12:19:20   

ok maybe reading the doco first is a good idea wink I need to set LivePersistance to false. It's now calling the event handler code but the PrefetchPath of the PerformSelectEventArgs2 object in the parameters is null.


    protected void ldsProduct_PerformSelect(object sender, SD.LLBLGen.Pro.ORMSupportClasses.PerformSelectEventArgs2 e)
    {
        e.PrefetchPath.Add(ProductEntity.PrefetchPathManufacturer);
    }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 19-Jun-2006 13:10:09   

The stacktrace suggests it can't bind to a control because it's not there confused ... It's not inside a control... weird.

The prefetch paths are null, unless you've set them in the code behind in the page_load event (first time code ends up there is sufficient)

Frans Bouma | Lead developer LLBLGen Pro
louieo avatar
louieo
User
Posts: 9
Joined: 24-May-2006
# Posted on: 20-Jun-2006 02:24:28   

Got it now! Thanks for your help. I needn't have worried about the PerformSelect handler, just needed to set the prefetch in the page load as you said and all works sweet simple_smile