Solved

get firstname lastname using userid

Posted on 2010-08-30
6
665 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 500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now