views:

220

answers:

2

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>
A: 

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

cdonner
+2  A: 

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; 
}
Phaedrus
That worked perfectly (after converting it to vb). Thank you sooooooo much! I was really close to that solution, just couldn't get it. Thanks Phaedrus, you rock! Wooo Hooo!!!!!
Cronner
@Cronner: Don't forget to mark this as the accepted answer.
Andy West