0

I want to compare the email with the parameter variable also called email. I am getting an error saying "Parameter ?email must be defined" I also tried using @email:

internal bool ValidateUser(string email, string password, bool rememberMe)
{
    bool valid = false;

    using (_msqlCon = new MySqlConnection(_connectionString))
    {
        _msqlCon.Open();
        _query = "SELECT Password, RememberMe FROM RegisteredUsers WHERE Email = ?email";

        using (_command = new MySqlCommand(_query, _msqlCon))
        {
            MySqlDataReader reader = _command.ExecuteReader();
            if (reader["Password"].Equals(password)) { valid = true;  }
        }
    }
    return valid;
}

How can I check if the column Email is the same as the one given as parameter?

1 Answer 1

1

If you're asking how you use the email parameter in the query, the answer is that you add it to the command object as a parameter. Try this:

internal bool ValidateUser(string email, string password, bool rememberMe)
{
    bool valid = false;
    using (_msqlCon = new MySqlConnection(_connectionString)) {
        _msqlCon.Open();
        _query = "SELECT Password, RememberMe FROM RegisteredUsers WHERE Email = @Email";

        using (_command = new MySqlCommand(_query, _msqlCon)) {
            _command.Parameters.AddWithValue("@Email", email);
            MySqlDataReader reader = _command.ExecuteReader();

            if (reader["Password"].Equals(password)) {
                valid = true;
            }
        }
    }
    return valid;
}

See the documentation for more information on how to use parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually my posted solution didn't behave as I expected, so I deleted it. My solution resulted in weird behavior with the MySqlDataReader. So your solution was the one I needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.