Reputation: 2823
Ok i have a insert statement that inserts into a table Rented it stores the dvdID
retrieved via a DG_Latest.SelectedRow.Cells[1].Text
,
I also want to collect the UserId
and store it into the Rented
table.
How would i go about obtaining the logged in User ID
My Code
da.InsertCommand = new OleDbCommand("INSERT INTO Rented (DVDID, UserID) VALUES (@rent, @user)", conn);
da.InsertCommand.Parameters.AddWithValue("@rent", DG_Latest.SelectedRow.Cells[1].Text);
da.InsertCommand.Parameters.AddWithValue("@user", ???);
Upvotes: 2
Views: 1475
Reputation: 77926
This should do it
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
(OR)
string userName = Environment.UserName
Well in case you want to get the userid then then below code line should work fine
Membership.GetUser().ProviderUserKey
Upvotes: 1
Reputation: 7943
Here it is:
var currentUser = Membership.GetUser(User.Identity.Name);
string username = currentUser.UserName //** get UserName
Guid userID = currentUser.ProviderUserKey //** get user ID
From this thread.
And add like this:
da.InsertCommand.Parameters.AddWithValue("@user", userID );
Upvotes: 1