How to get the USERID

D

Doug Batchelor

I am using the ASP.NET membership stuff and have succesfully implemented it
on my site. However, I would now like to be able to get the UserId which is
stored for registered users in the aspnet_users table in ASPNETDB.MDF. The
reason is that I would like to include this value for users who insert
records in other tables in my database. I thought I could use the LoginName
control to get the Username which I could then use to query the aspnet_users
table, but I can't find a property which will give me the Username in the
control.

I would appreciate any help from the gurus out there.
 
K

Ken Cox [Microsoft MVP]

Hi Doug,

Can you get it from the Profile? If so, here's some code that might get you
going.

Let us know?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<%@ import namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsNothing(Profile.UserName) Then
SqlDataSource1.SelectParameters.Item _
("UserName"). _
DefaultValue = Profile.UserName
Else
SqlDataSource1.SelectCommand = ""
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Get the UserID from ASPNET users database [Ken Cox]</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:listbox id="ListBox1" runat="server" datasourceid="SqlDataSource1"
datatextfield="Uid"></asp:listbox></p>
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:ASPNETDBConnectionString %>"
selectcommand="SELECT UserId AS Uid FROM vw_aspnet_Users WHERE (UserName =
@UserName)">
<selectparameters>
<asp:parameter name="UserName" />
</selectparameters>
</asp:sqldatasource>
&nbsp;</div>
</form>
</body>
</html>
 
D

Dominick Baier

I guess you are talking about the GUID?

You can get that from MembershipUser.ProviderUserKey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top