title: Asp.net login with SQL Server database
body: <p>I am trying to make a simple login form in my asp.net page(I had a html page and now I am trying to make it to asp.net). It should take the entered username and password and connect to the mssql database. There I will make a SQL Select statment which selects the username and  the password where the username is equal to the username entered in the textbox(and after that compare if the password entered is the same as the one in the database). If the username and the password is okay, then it should open another page - main.aspx. My index.aspx looks like this:</p>
  
  <pre><code>&lt;form id="loginform" runat="server" method="post" action="/"&gt;
  &lt;div id="username_box"&gt;&lt;p style="padding-top: 10px; margin: 0px"&gt;Username:&lt;/p&gt;&lt;/div&gt;
  &lt;input type="text" value="" name="usernameTextBox" runat="server" id="username_input" /&gt;&lt;br/&gt;
  &lt;div id="password_box"&gt;&lt;p style="padding-top: 10px; margin: 0px"&gt;Password:&lt;/p&gt;&lt;/div&gt;
  &lt;input type="password" value="" name="passwordTextBox" runat="server" id="password_input" /&gt;&lt;br/&gt;
  &lt;a href="facebook.com" style="float: left;padding-left: 13px;padding-top: 8px;"&gt;Glemt kodeordet?&lt;/a&gt;
  &lt;input type="submit" runat="server" id="LoginBtnClick" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>After the submit button is pressed, there is my c# code:</p>
  
  <pre><code>protected void LoginBtn_Click(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
      con.Open();
      SqlCommand cmd = new SqlCommand("select user_username, user_password FROM users WHERE user_username =@username and user_password=@password", con);
      cmd.Parameters.AddWithValue("@username", "user");
      cmd.Parameters.AddWithValue("@password", "1234");
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataTable dt = new DataTable();
      da.Fill(dt);
      if (dt.Rows.Count &gt; 0)
      {
          Response.Redirect("main.aspx");
      }else
      {
          ClientScript.RegisterStartupScript(Page.GetType(), "validation", "&lt;script language='javascript'&gt;alert('Invalid Username and Password')&lt;/script&gt;");
      }
  } 
  </code></pre>
  
  <p>Right now I am not using the username and the password that the user types in the textbox, but just a hardcoded username "user" and password "1234", which I have inserted in my database. I don't know how to get the text from the textbox. But even with the hardcoded username and password, this whole thing doesn't work.</p>
  
  <p>And now in the end, this is what I added in my Web.Config to make the connection string:</p>
  
  <pre><code>&lt;connectionStrings&gt;
  &lt;add name="myConnectionString" providerName="System.Data.SqlClient"  connectionString="Data Source=hereistheipofmydb;Initial Catalog=mydb;User Id =myadmin;password=mypass;" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>One more question: is it necessary the whole project to be uploaded on the server(the same as database)? Right now I have my database on the hosting, but the website I am running through Visual Studio 2012.</p>