Search code examples
asp.netparametersinsertdetailsviewuserid

Getting a UserId into a SQLDataSource


I am still new to asp.net and I'm having a problem that I just can't figure out. I'm using vb and the .net membership api.

My question is, how do I get the current user's userid into a DetailsView INSERT?

<InsertParameters>
<asp:Parameter Name="UserID"/>
</InsertParameters>

Solution

  • In the OnInserting event of your SqlDataSource you can retrieve the UserID from the Membership table by using the ProviderUserKey property of the MembershipUser class. You can then programmatically assign this value to the UserID insert parameter.

    protected void SqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e) 
    {      
        MembershipUser currentUser = Membership.GetUser();      
        Guid id = (Guid)currentUser.ProviderUserKey;
        e.Command.Parameters["@UserID"].Value = id; 
    }