title: Website login using C# to connect with existing sql server db
body: <p>I'm a brand newbie...</p>
  
  <p>I am attempting to log on to my website in VS2010, which connects to an existing SQL Server 2008 Express database through asp.net, utilizing C# as the code behind.  </p>
  
  <p>Here's my <code>login.aspx.cs</code> code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.IO;
  using System.Net.Mail;
  using System.Data.SqlClient;
  using System.Web.Configuration;
  
  public partial class Login : BasePage
  {
      protected void Page_Load(object sender, EventArgs e)
      {
      }
  
      protected void btnlogin_Click(object sender, EventArgs e)
      {
          int Results = 0;
  
          if (txtUsername.Text != string.Empty &amp;&amp; txtPassword.Text != string.Empty)
          {
  
              Results = Validate_Login(txtUsername.Text.Trim(), txtPassword.Text.Trim());
  
              if (Results == 1)
              {
                  lblMessage.Text = "Login is Good, Send user to another page or enable controls.";
              }
              else
              {
                  lblMessage.Text = "Username or Password is incorrect.";
                  lblMessage.ForeColor = System.Drawing.Color.Red;
              }
          }
          else
          {
              lblMessage.Text = "Please make sure that your username and password is correct.";
          }
      }
  
      protected int Validate_Login(String Username, String Password)
      {
          SqlConnection con = new SqlConnection(@"Server=MARIOM-PC\SQLEXPRESS;Database=Logon");
  
          SqlCommand cmdselect = new SqlCommand();
  
          cmdselect.CommandType = System.Data.CommandType.StoredProcedure;
  
          cmdselect.CommandText = "[dbo].[prcLoginv]";
  
          cmdselect.Parameters.Add("@Username", System.Data.SqlDbType.VarChar, 50).Value = Username;
          cmdselect.Parameters.Add("@Password", System.Data.SqlDbType.VarChar, 50).Value = Password;
          cmdselect.Parameters.Add("@OutRes", System.Data.SqlDbType.Int, 4);
          cmdselect.Parameters["@OutRes"].Direction = System.Data.ParameterDirection.Output;
  
          cmdselect.Connection = con;
  
          int Results = 0;
  
          try
          {
              con.Open();
  
              cmdselect.ExecuteNonQuery();
  
              Results = (int)cmdselect.Parameters["@OutRes"].Value;
          }
          catch (SqlException ex)
          {
              lblMessage.Text = ex.Message;
          }
          finally
          {
              cmdselect.Dispose();
  
              if (con != null)
              {
                  con.Close();
              }
          }
  
          return Results;
      }
  }
  </code></pre>
  
  <p>When I click my Log In button, it takes me to my C# code behind which should iterate through the <code>btn_Login_Click</code>, then the <code>Validate_Login</code> method. But then it does not correctly update my login page with the correct information. I always get the "incorrect password" error. </p>
  
  <p>Please help!</p>