Solved

get firstname lastname using userid

Posted on 2010-08-30
6
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

628 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