Solved

get firstname lastname using userid

Posted on 2010-08-30
6
667 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

863 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

28 Experts available now in Live!

Get 1:1 Help Now