Question

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>
Was it helpful?

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; 
}

OTHER TIPS

User.Identity.Name will get you the unique user name. You can find the id in the aspnet_Users table.

Just do it like this in one line:

protected void Sql_OnInserting(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters["@UserId"].Value = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top