I have an Async method in C# that is calling SQL function which is returning true or false and method is returning bool variable that is true or false.
This is my method:
public async Task<bool> Canlog(string userName, string pass
{
Context context = new Context();
bool queryResult = false;
using (context )
{
using (context .Database.Connection)
{
context .Database.Connection.Open();
string sqlStatment = .....
queryResult = authorizationContext.Database
.SqlQuery<bool>(sqlStatment)
.Single();
}
}
return await queryResult;
}
when I try this, I am getting error in this line return await queryResult that cannot await bool.
Single() returns <T>, not Task<T>. What you want is SingleAsync():
return await authorizationContext.Database
.SqlQuery<bool>(sqlStatement)
.SingleAsync();
Also, you can't have your await outside of your using statements because then your DB context will be disposed before the async operation is compelte.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With