Results 1 to 1 of 1

Thread: Validate Credentials Against User Record in Database

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Validate Credentials Against User Record in Database

    VB version here.

    CSharp Code:
    1. SqlConnection connection = new SqlConnection("connection string here");
    2.  
    3. // Get the count of the records with matching user ID and password.
    4. SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM User WHERE UserID = @UserID AND Password = @Password",
    5.                                     connection);
    6.  
    7. // Add the parameters.  Values might come from TextBoxes or wherever.
    8. command.Parameters.AddWithValue("@UserID", userID);
    9. command.Parameters.AddWithValue("@Password", password);
    10.  
    11. connection.Open();
    12.  
    13. // Execute the query.
    14. if ((int)command.ExecuteScalar() == 0)
    15. {
    16.     // Zero matching records means a failed login.
    17. }
    18. else
    19. {
    20.     // The specified credentials do match a record so the login succeeds.
    21. }
    22.  
    23. connection.Close();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width