?
Solved

get firstname lastname using userid

Posted on 2010-08-30
6
Medium Priority
?
677 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 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
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
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.
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.

719 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