title: How do I Validate a User Using LINQ to SQL?
body: <p>I'm trying to work out how to validate a user in my application but i don't know how.
  I wish create a form with Username and Password to enter in the application(WPF) using Linq toSql to store and validate Username and password in the database but i don't know so much about Linq to Sql so i keep in stuck now.
  I have a database with this fields"UserID,UserTypeID,Name,Surname,Username,Password,Email" 
  and here there is the code that i use :</p>
  
  <pre><code>public Window1()
      {
          InitializeComponent();
  
      }
  
      public bool ValidateApplicationUser(string userName, string password)
      {
          bool validUser = false;
          try
          {
           //   var conn = "Data Source=MAMMA-PC/SQLMAMMA;Initial Catalog=MyWebSite;Integrated Security=True";
              DataClasses1DataContext dc = new DataClasses1DataContext();
              Table&lt;User&gt; users = dc.GetTable&lt;User&gt;();
  
              var query = from c in dc.Users
                          where (c.Username == userName.ToLower() &amp;&amp; c.Password == password.ToLower())
                          select c;
  
              validUser = (query != null);
  
          }
          catch (Exception ex)
          {
              if (ex != null)
              {
              }
          }
  
          return validUser;
      }
  
      private void mahhh(object sender, RoutedEventArgs e)
      {
  
  
  
          bool authenticated = true;
       //   var conn = "Data Source=MAMMA-PC/SQLMAMMA;Initial Catalog=MyWebSite;Integrated Security=True";
              DataClasses1DataContext dc = new DataClasses1DataContext();
              Table&lt;User&gt; users = dc.GetTable&lt;User&gt;();
  
              var query = from c in dc.Users
                          where (c.Username == usernameTextBox.Text.ToString() &amp;&amp; c.Password == passwordTextBox.Text.ToString())
                          select c;
  
        /*      foreach (User user in query)
              {
                  user.Username = usernameTextBox.Text.Trim();
                  user.Password = passwordTextBox.Text.Trim();
              } */
  
  
          {
              if (usernameTextBox.Text !="" &amp;&amp; passwordTextBox.Text != "")
              {
                  authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text);
              }
  
          }
          if (!authenticated)
          {
              MessageBox.Show("Invalid login. Try again.");
          }
          else
          {
              MessageBox.Show("Congradulations! You're a valid user!");
  
  
      //        Window2 c = new Window2();
      //        c.ShowDialog();
              this.Close();
          }
  
      }
  </code></pre>
  
  <p>When i insert the password and username the form validate also wrong username and password so i receive always the message "Congradulations! You're a valid user!"!!!</p>
  
  <p>DO you have any advice where i wrong?</p>
  
  <p>Bye</p>