Failed to pass entity property as parameter in Mysqlcommand

Asked

Viewed 33 times

1

I have the following code in my application:

public Usuario checkLogin(Usuario entity)
    {
        //return
        Usuario userEntity = new Usuario();

        connection = mysql.OpenConnection();
        try
        {
            MySqlCommand stm = new MySqlCommand();
            MySqlDataReader data;

            stm.CommandText = "CALL validaLogin(?login, ?senha)";
            stm.Connection = connection;
            stm.Parameters.AddWithValue("?login", entity.Login);
            stm.Parameters.AddWithValue("?senha", entity.Senha);

            var id = stm.ExecuteScalar();
            errorUtil.showCustomAlert(id.ToString());
        } catch(MySqlException ex)
        {
            errorUtil.showDBError(ex);
        } finally{
            connection.Close();
        }
        return userEntity;
    }

Its function is to basically receive a user and password and perform login validation through stored Procedure validaLogin. However, when passing the attribute Login of the object entity my query simply not returned anything (necessarily need to return because the data I type in the form are identical to those saved in Mysql).

However, if I replace the object attribute with a string (as below) I get success in my query.

stm.Parameters.AddWithValue("?login", "logindousuario");
stm.Parameters.AddWithValue("?senha", "senhadousuario");

1 answer

1

I used the method Trim() to remove the spacing of the recovered text from the TextBox and it all worked out. Problem solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.