2
I have a method called consulta_usuario_email, that is responsible for searching in the table of registered users, the emails of users of a certain department:
public async Task<List<string>> consulta_usuario_email(string departamento)
{
consql.bd_string();
SqlConnection sqlconn = new SqlConnection(consql.sqlconn);
List<string> email = new List<string>();
try
{
consql._sql = @"select a.Email
from AspNetUsers as a
left join Empresa_departamento as c
on a.Departamento = c.id
where c.departamento = @departamento";
SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);
cmd.Parameters.Add("@departamento", SqlDbType.VarChar).Value = departamento;
await sqlconn.OpenAsync();
using (SqlDataReader leitor = await cmd.ExecuteReaderAsync())
{
while (leitor.Read())
{
email.Add(leitor["Email"].ToString());
}
}
}
catch (Exception error)
{
MessageBox.Show("Erro" + "\n" + error);
}
finally
{
sqlconn.Close();
}
return email;
} // consulta o email do usuario conforme seu departamento
I want to execute that method Async, and then when this is completed, I want to execute another method that receives as parameters the List generated by the method consulta_usuario_email
Task<List<string>> Mail_PCP = Email_User.consulta_usuario_email("PCP");
if (Mail_PCP.IsCompleted == true)
{
Mail_Urgente.Send(Mail_Compras, Mail_PCP, "Teste", "Teste");
}
However, how do I pass the List for the method Send class Mail_Urgente?
Update - ref. employee response @LINQ:
"Just wear await, like this:"
List<string> Mail_PCP = await Email_User.consulta_usuario_email("PCP"); Mail_Urgente.Send(Mail_Compras, Mail_PCP, "Teste", "Teste");
When trying to do as above the error is generated:

friend, but thus is generated an error... I made a review on the question adding a picture of the error.
– Thomas Erich Pimentel
I don’t see any mistake.
– Jéf Bueno
I just updated the question
– Thomas Erich Pimentel
The error is self-explanatory. You have to mark the method called with
async.– Jéf Bueno
But the method
consulta_usuario_emailis already marked asAsyncas can be seen at the beginning of the question.– Thomas Erich Pimentel
The calling method needs to be marked as
async. It was my typo.– Jéf Bueno
Perfect. I get it. Thank you.
– Thomas Erich Pimentel