Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
I have been trying to get SqlDataReader to work using Async/Awaits but without much success. I got IEnumerable, cannot convert Task to SqlDataReader and some other errors.

Here is basically my code and I must thank in advance to anybody who can really lend a hand to help out.

//App_Code class 

public class Customerslib
{
    public static SqlDataReader Customer_Select(Int32 Year, string Country, string Category)
    {

        SqlDataReader reader = null;
        var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand sqlcmd = new SqlCommand(Customer_Select,connection);
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.Parameters.Add(new SqlParameter("@Year", SqlDbType.Int)).Value = Year;
        sqlcmd.Parameters.Add(new SqlParameter("@Country", SqlDbType.VarChar,50)).Value = Country;
        sqlcmd.Parameters.Add(new SqlParameter("@Category", SqlDbType.NVarChar, 100)).Value = Category;
        sqlcmd.Connection = connection;
        connection.Open();
        reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        return reader;

    }

}


//ASPX page
 protected void Page_Load(object sender, EventArgs e)
    {   

        if (!IsPostBack)
        {
            using (SqlDataReader custreader = Customerslib.Customer_Select(Convert.ToInt16("2017"), "Thailand", "Manager")
            {
                rptCustomer.DataSource = custreader 
                rptCustomer.DataBind();
            }
        }
    }


What I have tried:

Have been trying these few links without much success due to the way I return my SQLDatareader.

c# - Return data from datareader asynchronously - Stack Overflow[^]

Async/Await Could Be Better[^]

c# - Using async / await with DataReader ? ( without middle buffers!) - Stack Overflow[^]
Posted
Updated 25-Aug-17 17:42pm

1 solution

Those links are one side of the solution, the other is how to handle async calls in ASP.NET Webforms.

I used this search: asp.net async webforms - Google Search[^] and found these two very helpful links for you:

* Using Asynchronous Methods in ASP.NET 4.5 | Microsoft Docs[^]
* The Magic of using Asynchronous Methods in ASP.NET 4.5 plus an important gotcha - Scott Hanselman[^]
 
Share this answer
 
Comments
George Swan 26-Aug-17 2:13am    
This link is also useful
http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4
Graeme_Grant 26-Aug-17 2:38am    
That is for MVC and not WebForms as used by the OP. MVC is very different.
George Swan 26-Aug-17 2:49am    
Good point, Graeme. But isn't the pattern for using async methods similar? I must confess that I have never used WebForms.
Graeme_Grant 26-Aug-17 3:21am    
Yes but the WinForm implementation is different to MVC. Compare the link you provided with the ones that I have and you will see how.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900