Failed to pass ownership of an entity as a parameter in MySqlCommand

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;
    }

The function of it is basically to receive a user and password and to carry out the validation of the login through stored procedure validaLogin . However, when I pass the Login attribute of the object entity my query is simply not returned at all (it must necessarily return because the data I type in the form is identical to the one saved in MySQL).

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

stm.Parameters.AddWithValue("?login", "logindousuario");
stm.Parameters.AddWithValue("?senha", "senhadousuario");
    
asked by anonymous 21.01.2018 / 23:14

1 answer

1

I used the Trim() method to remove the spacing of the text retrieved from TextBox and did everything right. Problem solved!

    
22.01.2018 / 01:27