user1960836
user1960836

Reputation: 1782

Compare a MySQL column with parameter variable from method

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?

Upvotes: 0

Views: 86

Answers (1)

jpw
jpw

Reputation: 44871

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.

Upvotes: 1

Related Questions