Displaying Users Name

I have an urgent issue that I need to fix. At this time, my web page (written in Asp.net/C#) displays their Windows Login ID on the top of the web page.  For example, when I visit the web page, it says "Hello Domain\UserID".  

I wanted to ask if there is a way that I can have it say the users name instead.  The web page uses the ASP.NET security tool and is configured for a local network.  I have posted the code that displays the UserID below.  I am hoping that there are some built in functions to display the name in ASP.NET.

 If it is not possible to obtain the name using the built in security/roles function in ASP. net, could you please let me know how I can get the UserID returned to a query and bring it back to the page?  I have a query that can take the userID and get the user's name.  I would need step-by-step instructions.

<LoggedInTemplate>
               <div id="welcomebox">
                  <asp:LoginName ID="LoginName1" runat="server" FormatString="Hello	{0}" meta:resourcekey="LoginName1Resource1" /><br />
                  <small>
          
               </div>
            </LoggedInTemplate>

Open in new window

LVL 1
jjrr007Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
Hi there,

To get the user name, smiply use this:

Dim userName As String
If HttpContext.Current.Request.IsAuthenticated Then
   userName = HttpContext.Current.User.Identity.Name
End If

Open in new window

jjrr007Author Commented:
Thanks.  that returned the WindowsID.  I am creating the from using C#/Asp.net.  
Juan_BarreraCommented:
No, that's the UserName for the user currently authenticated, not the Windows ID. The WindowsID is a SID value.
 
The get the UserId (using the ASP.NET security), you would use the following:

Dim guid as Guid =   Ctype( Membership.GetUser.ProviderUserKey, Guid)

Another way to get the user name (what you call WindowsID), is:

Dim userName as String =   Membership.GetUser.UserName

If that line returned "DOMAIN\USERNAME" is because that's just what the current user name is.
Now, are you storing the "real" user first and last names somewhere else (like, Profile)? Is that what you need?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

jjrr007Author Commented:
We are getting closer, but not there yet.  I have a query that can show the employee Name and takes what is returned by HttpContext.Current.User.Identity.Name.  
The problem that I am facing now is that when the Detail View loads, it can't get the default value (from HttpContext.Current.User.Identity.Name).  This is how I am trying to send the default value by this code:
 protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters.Add("User", HttpContext.Current.User.Identity.Name);
}
What do you suggest that I change?  Thanks
Juan_BarreraCommented:
I think that you want a SelectParamenter, not an UpdateParameter:

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("User", HttpContext.Current.User.Identity.Name);
}

Open in new window

jjrr007Author Commented:
Thanks.  When I did that, I received the following message:
Error 10 The name 'SqlDataSource1' does not exist in the current context C:\.master.cs
I am using a master page, does that make a difference?  What do you suggest or maybe another alternative way of doing this?
 
Juan_BarreraCommented:
Where is your SqlDataSource located?
Just add the above line t othe page / masterpage where the DS is located.
jjrr007Author Commented:
Here is the code, the datasource code is highlighed.  I pasted from the div item to the top of the page.  I pasted the c# code in the snippet.  I generated the error with the code.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Template.master.cs" Inherits="MB.NEAreaBusinessEfficiencyTeamWebForum.UI.Template" %>
<%@ Register Src="Controls/ThemeSelector.ascx" TagName="ThemeSelector" TagPrefix="mb" %>
<%@ Register Src="Controls/PollBox.ascx" TagName="PollBox" TagPrefix="mb" %>
<%@ Register Src="Controls/PersonalizationManager.ascx" TagName="PersonalizationManager" TagPrefix="mb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>NE Area Business Efficiency Team</title>
</head>
<body>
<form id="Main" runat="server">
<div id="header">
<div id="header2">
<div id="headermenu" > 

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:Menu ID="mnuHeader" runat="server" CssClass="headermenulink" DataSourceID="SiteMapDataSource1"
MaximumDynamicDisplayLevels="0" Orientation="Horizontal" SkipLinkText="" StaticDisplayLevels="2" Style="text-align: center; margin-left: 90px;">
</asp:Menu>
</div>
</div>
<div id="loginbox">
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="Login" runat="server" Width="100%" FailureAction="RedirectToLoginPage" meta:resourcekey="LoginResource1">
<LayoutTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="60px"><asp:Label runat="server" ID="lblUserName" AssociatedControlID="UserName" Text="Username:" meta:resourcekey="lblUserNameResource1" /></td>
<td><asp:TextBox id="UserName" runat="server" Width="95%" meta:resourcekey="UserNameResource2" /></td>
<td width="5px" align="right">
<asp:RequiredFieldValidator ID="valRequireUserName" runat="server" SetFocusOnError="True"
ControlToValidate="UserName" Text="*" ValidationGroup="Login" meta:resourcekey="valRequireUserNameResource1" />
</td>
</tr>
<tr>
<td><asp:Label runat="server" ID="lblPassword" AssociatedControlID="Password" Text="Password:" meta:resourcekey="lblPasswordResource1" /></td>
<td><asp:TextBox ID="Password" runat="server" TextMode="Password" Width="95%" meta:resourcekey="PasswordResource2" /></td>
<td width="5px" align="right">
<asp:RequiredFieldValidator ID="valRequirePassword" runat="server" SetFocusOnError="True"
ControlToValidate="Password" Text="*" ValidationGroup="Login" meta:resourcekey="valRequirePasswordResource1" />
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><asp:CheckBox ID="RememberMe" runat="server" Text="Remember me" meta:resourcekey="RememberMeResource1"></asp:CheckBox></td>
<td align="right">
<asp:ImageButton ID="Submit" runat="server" AlternateText="Login"
CommandName="Login" ImageUrl="~/images/go.gif" ValidationGroup="Login" meta:resourcekey="SubmitResource1" />
</td>
<td width="5px" align="right">&nbsp;</td>
</tr>
</table>

</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
<div id="welcomebox">
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
&nbsp;<asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbWebForumConnectionString %>"
SelectCommand="tbh_Forums_GetUserName" SelectCommandType="StoredProcedure">

</asp:SqlDataSource>

<asp:LoginName ID="LoginName1" runat="server" FormatString="Hello {0}" meta:resourcekey="LoginName1Resource1" /><br />
<small>
</div>
</LoggedInTemplate>
</asp:LoginView>

</div>
</asp:PlaceHolder>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
namespace NameTakenOutforPrivacy.UI
{
    public partial class Template : System.Web.UI.MasterPage
    {
     
 
        private bool _enablePersonalization = false;
        public bool EnablePersonalization
        {
            get { return _enablePersonalization; }
            set
            {
                _enablePersonalization = value;
 
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters.Add("User", HttpContext.Current.User.Identity.Name);
        }
 
        protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
 
        }
        protected void PollBox1_Load(object sender, EventArgs e)
        {
 
        }
}
}

Open in new window

Juan_BarreraCommented:
Place the datasource outside the template, like here:

</asp:LoginView> 
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" "ConnectionString="<%$ ConnectionStrings:dbWebForumConnectionString %>"
SelectCommand="tbh_Forums_GetUserName" SelectCommandType="StoredProcedure">
</asp:SqlDataSource> 
</div> 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjrr007Author Commented:
That worked! I will remember to place the data sets outside of the templates from now on.  Thanks again!
jjrr007Author Commented:
I wanted to thank you for your assistance, time and expertise with this question and others.  You are extremely knowledgeable!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.