SELECT COUNT on a Table w/ LLBLGen 2.0

Posts   
 
    
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 28-Jul-2006 05:23:26   

Hi all,

In read through the forums and it was recommended to use the GetDBCount function to retrieve a COUNT ona table.

Here is my query code:


    Public Shared Function GetCount(ByVal MaxRank As Integer) As Integer
        Using _Adapter As New DataAccessAdapter()
            Dim _DBCount As Integer = _Adapter.GetDbCount(New HelperClasses.EntityCollection(Of LeaderboardEntity), Nothing)
            If _DBCount >= MaxRank Then
                Return MaxRank
            Else
                Return _DBCount
            End If
        End Using
    End Function

I like to do a count on a flat table (i.e. no relations, no filters, etc).

I am getting an exception on the line


Dim _DBCount As Integer = _Adapter.GetDbCount(New HelperClasses.EntityCollection(Of LeaderboardEntity), Nothing)

Any ideas why I get the following error:

System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." Source="SD.LLBLGen.Pro.ORMSupportClasses.NET20" StackTrace: at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.GetDbCount(IEntityCollection2 collection, IRelationPredicateBucket filter) at CBO.BusinessLayer.LeaderBoard.GetCount(Int32 MaxRank) in D:\Personal Programming Projects\DateOMatic\CBO.BusinessLayer\LeaderBoard.vb:line 21 at LeaderBoard.LeaderBoardDataSource_PerformGetDbCount(Object sender, PerformGetDbCountEventArgs2 e) in D:\Personal Programming Projects\DateOMatic\Web Application\LeaderBoard.aspx.vb:line 33 at SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSource2.OnPerformDbCount(PerformGetDbCountEventArgs2 eventArgs) at SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView2.ExecuteSelectEntityCollection(Int32 pageSize, Int32 pageNumber, DataSourceSelectArguments arguments) at SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView2.ExecuteSelect(DataSourceSelectArguments arguments) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.GridView.DataBind() at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Seems like something within ORMSupportClasses is exceptioning?

mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 28-Jul-2006 05:31:26   

OK, I think I solved it... GetDBCount doesn't like a typed EntityCollecion.

If I switch:


Dim _DBCount As Integer = _Adapter.GetDbCount(New HelperClasses.EntityCollection(Of LeaderboardEntity), Nothing)

to


Dim _DBCount As Integer = _Adapter.GetDbCount(New HelperClasses.EntityCollection(New FactoryClasses.LeaderboardEntityFactory), Nothing)

It works... Is this a bug? Or am I using Generics wrong (I'm still new to .NET 2.0 and generics) cry

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 28-Jul-2006 08:03:50   

This also appears to work:


Dim amount As Integer = CInt(adapter.GetDbCount(New LeaderboardEntityFactory().CreateFields(), Nothing))

This is the way that it is shown in the docs under "Generated code - Entity collection and Typed List/Typed View paging, Adapter"

I find that this generates much smaller and I suspect faster SQL


Dim amount As Integer = CInt(adapter.GetScalar(LeaderboardFields.LeaderboardId, AggregateFunction.Count)))

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 28-Jul-2006 09:54:47   

You should set the entity factory of the newly created entitycollection to a value. simple_smile The factory is needed because it needs to create fields.

Frans Bouma | Lead developer LLBLGen Pro