5

I have a method in c#:

public DataSet OpenSqlQuery(string SqlQuery)
{
    con = new SqlConnection(connection_string);
    con.Open();
    var sqlc = new SqlCommand(SqlQuery, con);
    da = new SqlDataAdapter(sqlc);
    var ds = new DataSet();
    da.Fill(ds);
    con.Close();
    return ds;
}

Now I want to make it as an asynchronous method so I can call it by await. so I changed it to:

public async Task<DataSet> OpenSqlQuery(string SqlQuery)
{
    con = new SqlConnection(connection_string);
    con.Open();
    var sqlc = new SqlCommand(SqlQuery, con);
    da = new SqlDataAdapter(sqlc);
    var ds = new DataSet();
    await da.Fill(ds);
    con.Close();
    return ds;
}

I set await before da.Fill(ds) because at this line, the program waits for server. But the function shows this error:

'int' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

How can I make this method asynchronous?

P.S: The main goal is to call this method as follows:

Dataset ds = await objConnect.OpenSqlQuery(query);

P.S: Difference between this question and possible dupplicated one is that this post about making an existing synchronous method to asynchronous, while another is about making an asynchronous method.

Alireza Zojaji
  • 802
  • 2
  • 13
  • 33

0 Answers0