title: Login to authenticated users
body: <p>I have a 2 textbox for username and password and a button. I want to allow authenticated users to login and redirect to another page and display welcome authenticated username.Or else display the message Invalid username.How to perform this using session and connect to database.How to configure with database. Please help me.</p>
  
  <pre><code>My code is
  </code></pre>
  
  <p></p>
  
  <pre><code>        &lt;div id="loginbox"&gt;
              &lt;div align="center" style="width: 100%;"&gt;
  
  
              &lt;/div&gt;--%&gt;
              &lt;br /&gt;
  
                          &lt;label for="username"&gt;Username:&lt;/label&gt; &lt;input type="text"   
  id="username" /&gt;&lt;/li&gt;
     &lt;label for="password"&gt;Password:&lt;/label&gt; &lt;input type="password" id="password" /&gt;
  
  &lt;input type="button" runat="server" value="Login"&gt;
  
  
  &lt;/form&gt;
  
  My .cs code
  
  protected void btnLogin_onclick(object sender, EventArgs e)
  {
      string connect = "Data Source=ST0022;Initial Catalog=QuickMove_Globe;Persist   
  Security Info=True;User ID=sa;Password=good";
      string query = "Select Count(*) From Common.Users Where UserID = ? And Password =   
  ?";
      int result = 0;
      SqlConnection con= new SqlConnection(connect);
          SqlCommand cmd = new SqlCommand(query, con);
  
              cmd.Parameters.AddWithValue("UserID",username.Value);
              cmd.Parameters.AddWithValue("Password", password.Value);
              con.Open();
              Session["User"] = username.Value;
              result = (int)cmd.ExecuteScalar();
  
      if (result &gt; 0)
      {
          Response.Redirect("TestPortalPage.aspx");
      }
      else
      {
          lbluser.Text = "Invalid credentials";
      }
  
  
  }
  </code></pre>