Beep
Beep

Reputation: 2823

Insert UserID of Logged in user

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

Answers (2)

Rahul
Rahul

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

afzalulh
afzalulh

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

Related Questions