ReturnValue / OutputParameters

Feb 17, 2010 at 12:09 PM

Hi guys,

Is there anyway to get the Id of the newly inserted record? I have been trying through e.ReturnValue (null) and e.OutputParameters (0 elements in dictionary) in the OnInserted event of the

GenericObjectDataSource with no luck? I am assuming that Insert isnt returning anything so then how to i actually get the Id of the record?

 

Thanks again,

Pete

Feb 17, 2010 at 12:14 PM

After an Insert, the entity you've just inserted will now have a value populated on it's Id field.

So say you have a Company entity with fields: Id, Name, Size, etc. After Insert, the Id field (which was 0 or null before insert) will now have the new Id. Query it by Company.Id.

Feb 17, 2010 at 12:25 PM
Edited Feb 17, 2010 at 12:25 PM

Hi,

Thanks for the quick reply.

Ok so where would i access the entity? In which event? For example lets say i have

<dal:GenericObjectDataSource ID="GenericObjectDataSource1" runat="server" DataObjectTypeName="DataLayer.Activity"
TypeName="DataLayer.ActivityStaticRepository" 
    DeleteMethod="Delete" InsertMethod="Insert" 
    OldValuesParameterFormatString="original_{0}" SelectCountMethod="Count" 
    SelectMethod="GetAllUpcomingActivities" UpdateMethod="Update"
    OnInserting="GenericObjectDataSource1_OnInserting" OnInserted="GenericObjectDataSource1_OnInserted">
    <SelectParameters>
        <asp:Parameter Name="usersCurrentDate" Type="DateTime" DefaultValue="11-oct-2010"></asp:Parameter>
    </SelectParameters>
    <DeleteParameters>
<asp:Parameter Name="entity" Type="Object"></asp:Parameter>
<asp:Parameter DefaultValue="true" Name="submitChanges" Type="Boolean"></asp:Parameter>
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="entity" Type="Object"></asp:Parameter>
<asp:Parameter DefaultValue="true" Name="submitChanges" Type="Boolean"></asp:Parameter>
</UpdateParameters>
</dal:GenericObjectDataSource>

<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" DataKeyNames="Activity_Id" 
    DataSourceID="GenericObjectDataSource1" OnItemInserting="FormView1_OnItemInserting" OnItemInserted="FormView1_OnItemInserted">
    <InsertItemTemplate>
        ActivityName:
        <asp:TextBox ID="ActivityNameTextBox" runat="server" 
            Text='<%# Bind("ActivityName") %>' />
    </InsertItemTemplate>
</asp:FormView>

On the actual page, then in the CodeBehind i have

protected void GenericObjectDataSource1_OnInserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.Exception == null)
        {
            System.Web.HttpContext.Current.Trace.Write("" + e.OutputParameters.Count);
            if (e.OutputParameters != null && e.OutputParameters.Count > 0) //else throw exception?
                System.Web.HttpContext.Current.Trace.Write("" + e.OutputParameters["Activity_Id"].ToString());
        }        
    }

protected void FormView1_OnItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            //display an error message
        }
        else
        {
            System.Web.HttpContext.Current.Trace.Write(e.Values["Activity_Id"].ToString());
        }
    }

To cover the 2 inserted events of the GenericObjectDataSource and the FormView, both which do not work for me by the way. So which step am i missing?

Thanks,

Pete

Feb 17, 2010 at 12:50 PM

Maybe Tali can help with this one. Personally I don't use formviews anymore as there's too much junk surrounding the event model to make proper sense of what's actually going on.

One thing is for sure though, the entity is not in the OutputParameters because it's not an output parameter. It'll quite possibly be somewhere hidden in the eventArgs, but I just can't remember.

Feb 17, 2010 at 12:53 PM

Actually, let's be clear about what I'm talking about. Example code below:

var companyForUpdate = new Company() { Name="Some company", Details="Some details" };
DataLayer.CompanyStaticRepository.Insert(companyForUpdate);
int newId = companyForUpdate.Id;



Feb 17, 2010 at 12:56 PM

Yeah i can understand that one but i am using declarative binding so i dont have the entity as such as far as i know hidden in the eventargs but

thanks for the help will wait to see what Tali says then

Pete

Feb 19, 2010 at 2:10 PM
Hi Pete, I've made a change to the GenericRepository class (and interface) so that Insert now returns the primary key of the newly inserted entity. This means you can get it in the ObjectDataSource Inserted event from the ObjectDataSourceStatusEventArgs.ReturnValue property.
If you get the latest source version from CodePlex you should see the change along with an example of it's use in the Default.aspx page of the website project.
Cheers,
Rupert

On Wed, Feb 17, 2010 at 12:56 PM, saintpoida <notifications@codeplex.com> wrote:

From: saintpoida

Yeah i can understand that one but i am using declarative binding so i dont have the entity as such as far as i know hidden in the eventargs but

thanks for the help will wait to see what Tali says then

Pete

Read the full discussion online.

To add a post to this discussion, reply to this email (MultiTierLinqToSql@discussions.codeplex.com)

To start a new discussion for this project, email MultiTierLinqToSql@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Feb 22, 2010 at 1:56 AM

Hi Rupert,

Wow thanks that was quick too!!!

For some reason i wasnt notified of response but glad i checked today. Will give it a go later today

Thanks again,

Pete