How to get the "Last Inserted Record" from llblgendatasource2

Posts   
 
    
bunzee
User
Posts: 84
Joined: 20-Mar-2007
# Posted on: 19-Apr-2007 06:41:01   

Hi,

Using SqlDataSource, I can set the InsertParamater direction to output and retrieve the ID of the record that is just added. (see snippet below)

Could any one please kindly show me how can I achieve this using llblgendatasource2?

Thanks,

Snippet:

<asp:SqlDataSource ... OnInserted=”SqlDataSource1_Inserted”> <InsertParameters> ... <aspstuck_out_tongue_winking_eye arameter Direction=Output Name=”NewId” Size=4 Type=Int16 /> </InsertParameters> </asp:SqlDataSource>

...

protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) { object newId = e.Command.Parameters[“@NewId”].Value; ... }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Apr-2007 11:05:44   

To retrieve the PK from the code, you need to set LivePersistence to false, and handle the PerformWork event as follows:


        protected void LLBLGenProDataSource2_1_PerformWork(object sender, SD.LLBLGen.Pro.ORMSupportClasses.PerformWorkEventArgs2 e)
        {
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                e.Uow.Commit(adapter, true);
                
                List<ActionQueueElement<IEntity2>> insertQueue = e.Uow.GetInsertQueue();
                if (insertQueue.Count > 0) // in case of insert
                {
                    int id = ((CustomerEntity)insertQueue[0].Entity).Id;
                }
            }
        }
bunzee
User
Posts: 84
Joined: 20-Mar-2007
# Posted on: 20-Apr-2007 18:57:06   

Waala,

Thank you very much for the prompt response. It works like a charm.

One question: where can I find technical descriptions and examples for unitofwork objects. I have read the llblgen user guide document, llblgen reference document, and rapid c# windows developement book. Yet none of them talks about unitofwork in detail.

Thank you,

BZ

Posts: 254
Joined: 16-Nov-2006
# Posted on: 21-Apr-2007 21:47:50   

If you've read all of those resources in detail I can't think of many other places to get further information other than

a) Reviewing the LLBLGen source code. This is often very insightful and helps provides you with the deepest understanding of the code. The code is well written and well commented so I'm sure you would learn some good habits from this too wink

b) Searching on the forums using the UnitOfWork keywords. And of course searching on the internet using this keyword.