title: ASP.net Session login
body: <p>I'm trying to implement a session login into my web application. At the moment I'm receiving an error message "The name 'txtUserName' does not exist in the current context. When I check the aspx file the textbox ID="txtUserName" exists.</p>
  
  <pre><code>&lt;h2&gt;
      Log In
  &lt;/h2&gt;
  &lt;p&gt;
      Please enter your username and password.
      &lt;asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false"&gt;Register&lt;/asp:HyperLink&gt; if you don't have an account.
  &lt;/p&gt;
  &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&gt;Account Information&lt;/legend&gt;
                  &lt;p&gt;
                      &lt;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"&gt;Username:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="txtUserName" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                           CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                           ValidationGroup="LoginUserValidationGroup"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                      &lt;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"&gt;Password:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="txtPassword" 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&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" Onclick="LoginButton_Click" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/LayoutTemplate&gt;
  &lt;/asp:Login&gt;
  </code></pre>
  
  <p>My current code behind file is:</p>
  
  <pre><code>using System;
  using System.Data;
  using System.Configuration;
  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;
  using System.Data.SqlClient;
  using System.Web.Configuration;
  using System.Collections.Generic;
  
  namespace Project.Account
  {
      public partial class Login : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
          }
  
          protected void LoginButton_Click(object sender, EventArgs e)
          {
  
              string connectionString = WebConfigurationManager.ConnectionStrings["DBConnectionString1"].ConnectionString;
  
  
              SqlConnection con = new SqlConnection(connectionString);
              SqlCommand cmd = new SqlCommand("LoginUsers", con);
              cmd.CommandType = CommandType.StoredProcedure;
  
  
              cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 20));
              cmd.Parameters["@UserName"].Value = txtUserName.Text;
  
              cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 20));
              cmd.Parameters["@Password"].Value = txtPassword.Text;
  
  
                          }
      }
  }
  </code></pre>
  
  <p>txtUSerName.Text and txtPassword.Text are the one that have the underlined error</p>