Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

get firstname lastname using userid

Posted on 2010-08-30
6
Medium Priority
?
679 Views
Last Modified: 2012-05-10
Is it possible to get the First and lastname of a user using the userID, using the code below?

Currently I am getting error: Exception Details: System.FormatException: Input string was not in a correct format.
Which I believe is pointing to the User Provider Key.  What other way can I supply the UserID as Current User ID.
<LoggedInTemplate>
               <asp:SqlDataSource ID="UserNameDS" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:rmeaspnetdbConnectionString %>" 
                        SelectCommand="SELECT * FROM [aspnet_UserName] WHERE ([UserID] = @UserID)">
                <SelectParameters>
                        <asp:Parameter DefaultValue="<%=User.ProviderUserKey.ToString()%>" Name="UserID" Type="Int32" />
                </SelectParameters>
                </asp:SqlDataSource>                   
                Welcome <asp:FormView ID="UserNameFV" runat="server" DataKeyNames="UserID"
                      DataSourceID="UserNameDS" Height="21px" Width="185px">
                      <ItemTemplate>
                          <asp:Label ID="FirstName" runat="server" Text='<%#Bind("[FirstName]")%>' />
                          <asp:Label ID="Spacer" runat="server" Text=" " /> 
                          <asp:Label ID="LastName" runat="server" Text='<%#Bind("[LastName]")%>' />
                      </ItemTemplate>
                    </asp:FormView> |
                        <a href="/my-account/">My Account</a> |
                    
               </LoggedInTemplate>

Open in new window

0
Comment
Question by:Robert Treadwell
6 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33558732
Response.write the provideruserkey to check what value it has. If it is a non-numeric value, you need to consider an alternative to know the userID, you can also use the Username I suppose? That's User.Identity.Name
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1500 total points
ID: 33558863
try converting user provider key to integer first

<LoggedInTemplate>
               <asp:SqlDataSource ID="UserNameDS" runat="server"
                        ConnectionString="<%$ ConnectionStrings:rmeaspnetdbConnectionString %>"
                        SelectCommand="SELECT * FROM [aspnet_UserName] WHERE ([UserID] = @UserID)">
                <SelectParameters>
                        <asp:Parameter DefaultValue="<%=CInt(User.ProviderUserKey.ToString())%>" Name="UserID" Type="Int32" />
                </SelectParameters>
                </asp:SqlDataSource>                  
                Welcome <asp:FormView ID="UserNameFV" runat="server" DataKeyNames="UserID"
                      DataSourceID="UserNameDS" Height="21px" Width="185px">
                      <ItemTemplate>
                          <asp:Label ID="FirstName" runat="server" Text='<%#Bind("[FirstName]")%>' />
                          <asp:Label ID="Spacer" runat="server" Text=" " />
                          <asp:Label ID="LastName" runat="server" Text='<%#Bind("[LastName]")%>' />
                      </ItemTemplate>
                    </asp:FormView> |
                        <a href="/my-account/">My Account</a> |
                   
               </LoggedInTemplate>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 33558902
or try

<LoggedInTemplate>
               <asp:SqlDataSource ID="UserNameDS" runat="server"
                        ConnectionString="<%$ ConnectionStrings:rmeaspnetdbConnectionString %>"
                        SelectCommand="SELECT * FROM [aspnet_UserName] WHERE ([UserID] = @UserID)">
                <SelectParameters>
                        <asp:Parameter DefaultValue="<%=CInt(User.ProviderUserKey)%>" Name="UserID" Type="Int32" />
                </SelectParameters>
                </asp:SqlDataSource>                  
                Welcome <asp:FormView ID="UserNameFV" runat="server" DataKeyNames="UserID"
                      DataSourceID="UserNameDS" Height="21px" Width="185px">
                      <ItemTemplate>
                          <asp:Label ID="FirstName" runat="server" Text='<%#Bind("[FirstName]")%>' />
                          <asp:Label ID="Spacer" runat="server" Text=" " />
                          <asp:Label ID="LastName" runat="server" Text='<%#Bind("[LastName]")%>' />
                      </ItemTemplate>
                    </asp:FormView> |
                        <a href="/my-account/">My Account</a> |
                   
               </LoggedInTemplate>
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 5

Expert Comment

by:JayFromPep
ID: 33558984
What database are you using?  If you are using the default aspnet membership database created by visual studio, at least in mine, there is no table called aspnet_UserName.

Assuming you are using that, then your select statement should be...

SELECT * FROM aspnet_Users WHERE userid=@userid

That would return a user record from the table, wherein you would find the username.  However, it may or may not be the first and last name, depending on how you have your membership setup.

If you are using profiles, and have the first and last name recorded in the profile, then you could get at it that way.
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33559086
If you are using aspnet membership then userid is a GUID so change the datatype of userid from Int32 to GUID .

Happy Coding :)
0
 

Author Closing Comment

by:Robert Treadwell
ID: 33580636
UserID was also in uniqueidentifier format, added another column containing usernameID
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question