title: how to display user name in login name control
body: <p>I have a master page that holds the loginview content that appears on all subsequent pages based on the master page. i have a username control also nested in the loginview to display the name of the user when they are logged in. the code for the loginview from the master page is displayed as follows:</p>
  
  <pre><code>&lt;div class="loginView"&gt;
                  &lt;asp:LoginView ID="MasterLoginView" runat="server"&gt;
                      &lt;LoggedInTemplate&gt;
                          Welcome &lt;span class="bold"&gt;&lt;asp:LoginName ID="HeadLoginName" runat="server" /&gt; 
                              &lt;asp:Label ID="userNameLabel" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;&lt;/span&gt;!
                      [ &lt;asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/&gt; ]
                          &lt;%--Welcome: 
                          &lt;span class="bold"&gt;&lt;asp:LoginName ID="MasterLoginName" runat="server" /&gt; &lt;/span&gt;!--%&gt;                       
                      &lt;/LoggedInTemplate&gt;
                      &lt;AnonymousTemplate&gt;
                          Welcome: Guest
                          [ &lt;a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server"&gt;Log In&lt;/a&gt; ]
                      &lt;/AnonymousTemplate&gt;
  
                  &lt;/asp:LoginView&gt;
                  &lt;%--&amp;nbsp;&amp;nbsp; [&amp;nbsp;&lt;asp:LoginStatus ID="MasterLoginStatus" runat="server" LogoutAction="Redirect" LogoutPageUrl="~/Logout.aspx" /&gt;&amp;nbsp;]&amp;nbsp;&amp;nbsp;--%&gt;
  
              &lt;/div&gt;
  </code></pre>
  
  <p>Since VS2010 launches with a default login page in the accounts folder, i didnt think it necessary to create a separate log in page, so i just used the same log in page. please find the code for the login control below:</p>
  
  <pre><code> &lt;asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false"&gt;
      &lt;LayoutTemplate&gt;
          &lt;span class="failureNotification"&gt;
              &lt;asp:Literal ID="FailureText" runat="server"&gt;&lt;/asp:Literal&gt;
          &lt;/span&gt;
          &lt;asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
               ValidationGroup="LoginUserValidationGroup"/&gt;
          &lt;div class="accountInfo"&gt;
              &lt;fieldset class="login"&gt;
                  &lt;legend style="text-align:left; font-size:1.2em; color:White;"&gt;Account Information&lt;/legend&gt;
                  &lt;p style="text-align:left; font-size:1.2em; color:White;"&gt;
                      &lt;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"&gt;User ID:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="UserName" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                           CssClass="failureNotification" ErrorMessage="User ID is required." ToolTip="User ID field is required." 
                           ValidationGroup="LoginUserValidationGroup"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/p&gt;
                  &lt;p style="text-align:left; font-size:1.2em; color:White;"&gt;
                      &lt;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"&gt;Password:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                          TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                           CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                           ValidationGroup="LoginUserValidationGroup"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/p&gt;
                  &lt;p style="text-align:left; font-size:1.2em; color:White;"&gt;
                      &lt;asp:CheckBox ID="RememberMe" runat="server"/&gt;
                      &lt;asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline"&gt;Keep me logged in&lt;/asp:Label&gt;
                  &lt;/p&gt;
              &lt;/fieldset&gt;
              &lt;p class="submitButton"&gt;
                  &lt;asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                      ValidationGroup="LoginUserValidationGroup" onclick="LoginButton_Click"/&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/LayoutTemplate&gt;
  &lt;/asp:Login&gt;
  </code></pre>
  
  <p>I then wrote my own code for authentication since i had my own database. the following displays the code in the login buttons click event.:</p>
  
  <pre><code> public partial class Login : System.Web.UI.Page
  {
      //create string objects
      string userIDStr, pwrdStr;
  
      protected void LoginButton_Click(object sender, EventArgs e)
      {
  
  
          //assign textbox items to string objects
          userIDStr = LoginUser.UserName.ToString();
          pwrdStr = LoginUser.Password.ToString();
  
          //SQL connection string
  
          string strConn;
          strConn = WebConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ConnectionString;
  
          SqlConnection Conn = new SqlConnection(strConn);
  
  
  
          //SqlDataSource CSMDataSource = new SqlDataSource();
         // CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();
  
  
          //SQL select statement for comparison
  
          string sqlUserData;
          sqlUserData = "SELECT StaffID, StaffPassword, StaffFName, StaffLName, StaffType FROM Staffs";
          sqlUserData += " WHERE (StaffID ='" + userIDStr + "')";
          sqlUserData += " AND (StaffPassword ='" + pwrdStr + "')";
  
          SqlCommand com = new SqlCommand(sqlUserData, Conn);
          SqlDataReader rdr;
          string usrdesc;
          string lname;
          string fname;
          string staffname;
  
          try
          {
  
              //string CurrentData;
              //CurrentData = (string)com.ExecuteScalar();
              Conn.Open();
              rdr = com.ExecuteReader();
              rdr.Read();
              usrdesc = (string)rdr["StaffType"];
              fname = (string)rdr["StaffFName"];
              lname = (string)rdr["StaffLName"];
              staffname = lname.ToString() + " " + fname.ToString();
              LoginUser.UserName = staffname.ToString();
              rdr.Close();
  
              if (usrdesc.ToLower() == "administrator")
              {
  
                  Response.Redirect("~/CaseAdmin.aspx", false);
  
               }
               else if (usrdesc.ToLower() == "manager")
               {
                   Response.Redirect("~/CaseManager.aspx", false);
               }
               else if (usrdesc.ToLower() == "investigator")
               {
                   Response.Redirect("~/Investigator.aspx", false);
               }
               else
               {
                   Response.Redirect("~/Default.aspx", false);
               }               
  
  
          }
          catch(Exception ex)
          {
              string script = "&lt;script&gt;alert('" + ex.Message + "');&lt;/script&gt;";
          }
          finally
          {
              Conn.Close();
          }
  
  
      }
  </code></pre>
  
  <p>My authentication works perfectly and the page gets redirected to the designated destination. However, the login view does not display the users name. i actually cant figure out how to pass the users name that i had picked from the database to the login name control to be displayed.</p>
  
  <p>taking a close look i also noticed the logout text that should be displayed after successful log in does not show. that leaves me wondering if the loggedin template control on the masterpage even fires at all or its still the anonymous template control that keeps displaying.?</p>
  
  <p>How do i get this to work as expected? Please help....</p>