title: Login function using SQLCommand
body: <p>I am having prob when execute the code below. when my username and password true, i will get this message "Invalid User! Try again with VALID username and password". if i enter false username n password, nothing happens(still cannot proceed to next form). </p>
  
  <pre><code>private bool CompareStrings(string string1, string string2)
      {
          return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          try 
          {
              SqlConnection con = new SqlConnection();
              con.ConnectionString = "Server = blabla; Database = MoinMoun; User Id = Aema; password = 12345";
              con.Open();
  
              //SqlCommand cmd = new SqlCommand("SELECT username,password FROM Admin WHERE username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con);SELECT ISNULL(username,'') AS username, ISNULL(password,'') AS password FROM Admin WHERE username = '" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con
  
  
              SqlCommand cmd = new SqlCommand("SELECT username,password FROM Admin WHERE username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con);
  
              SqlDataReader dr = cmd.ExecuteReader();
  
              string usertext = txtusername.Text;
              string passtext = txtpassword.Text;
  
              while(dr.Read())
              {
  
                  if(this.CompareStrings(dr["username"].ToString(), usertext) &amp;&amp;
                      this.CompareStrings(dr["password"].ToString(), passtext))
                  {
                      Form2 frm = new Form2();
                      frm.Show();
                      this.Hide();                        
                  }
                  else
                  {       
                      MessageBox.Show("Invalid User! Try again with VALID username and password");                        
                  }
              }
              dr.Close();
              con.Close();
          }
          catch(Exception ex)
          {
              MessageBox.Show(ex.Message);
  
          }
      }
  </code></pre>