llblgenprodatasource Formview Entity ID

Posts   
 
    
Posts: 72
Joined: 11-Aug-2006
# Posted on: 03-Sep-2006 04:38:40   

Hello

I am making a registration form that is first in insert mode and then in update mode.

I'm trying to figure out how to use llblgenprodatasource ( self servicing ) to do this.

I set the default mode to insert mode and then when I want to save I execute the following code.

frmRego.InsertItem(false); frmRego.ChangeMode(FormViewMode.Edit);

I don't know how to obtain the id from the inserted item so that I can use it in the update ( using SQL trace it does return the scope_identity in SQL )

The MSDN example for SQL server use the onInserted event of the SQLDatasource.

What is the best way to acomplish this with LLBLGenprodatasource ( maybe I'm missing something in the docs )

Thanks heaps for your time.

Marty

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 03-Sep-2006 18:38:11   

Set the LivePersistence property of your datasource control to False. This will force you to manually execute the insert in code (use the PerformWork event of the datasource control). Then you can retrieve the PK of the newly inserted record.

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 04-Sep-2006 20:25:52   

Can you give a short code example of this?

I'm trying to do this same task but I don't know how to access the entity from the UnitOfWork.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 04-Sep-2006 21:38:25   

Code:


protected void OnPerformGetDbCount( object sender, PerformGetDbCountEventArgs2 e )
{
    int dbCount = 0;
    using( DataAccessAdapter adapter = new DataAccessAdapter() )
    {
        switch( e.DataContainerType )
        {
            case DataSourceDataContainerType.EntityCollection:
                dbCount = adapter.GetDbCount( e.ContainedCollection, e.Filter );
                break;
            case DataSourceDataContainerType.TypedList:
                dbCount = adapter.GetDbCount( e.Fields, e.Filter, null, e.AllowDuplicates );
                break;
            case DataSourceDataContainerType.TypedView:
                dbCount = adapter.GetDbCount( e.Fields, e.Filter );
                break;
        }
    }
    e.DbCount = dbCount;
}

protected void OnPerformSelect( object sender, PerformSelectEventArgs2 e )
{
    using( DataAccessAdapter adapter = new DataAccessAdapter() )
    {
        switch( e.DataContainerType )
        {
            case DataSourceDataContainerType.EntityCollection:
                adapter.FetchEntityCollection( e.ContainedCollection, e.Filter, e.MaxNumberOfItemsToReturn, e.Sorter, e.PageNumber, e.PageSize );
                break;
            case DataSourceDataContainerType.TypedList:
                adapter.FetchTypedList( e.Fields, (DataTable)e.ContainedTypedList, e.Filter, e.MaxNumberOfItemsToReturn, e.Sorter,
                        e.AllowDuplicates, null, e.PageNumber, e.PageSize );
                break;
            case DataSourceDataContainerType.TypedView:
                adapter.FetchTypedView( e.Fields, (DataTable)e.ContainedTypedView, e.Filter, e.MaxNumberOfItemsToReturn, e.Sorter, e.AllowDuplicates, null,
                        e.PageNumber, e.PageSize );
                break;
        }
    }
}


protected void llblgenDS2_PerformWork( object sender, PerformWorkEventArgs2 e )
{
    using( DataAccessAdapter adapter = new DataAccessAdapter() )
    {
        e.Uow.Commit( adapter, true );
        llblgenDS2.Refetch = true;
    }
}

Rule of thumb: the event args of the particular event handler is the container of all the parameters to pass to the method you need to call in the generated code, e.g. the dataaccessadapter method to fetch an entitycollection for example

Frans Bouma | Lead developer LLBLGen Pro