Bind (Populate) ASP.Net GridView asynchronously (Async Method) using C#

Last Reply 7 months ago By dharmendr

Posted 7 months ago

I have an article in which the author has explained to bind the data to gridview using asynchronous programming.

Using Async/Await Task Methods With SQL Queries .NET 4.5

But I am unable to bind it to just 1 simple gridview. But there is nothing happening to the page.

Please provide me the solution for this.

so far I have done this :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BSD
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetSomeData("select * from app_notifications");
                GridView1.DataBind();
            }
        }

        private string connection = "Data Source=.;Initial Catalog=xxxx;Integrated Security=True;Pooling=false;";

        private async Task GetSomeData(string sql)
        {
            //Use Async method to get data
            DataSet results = await GetDataSetAsync(connection, sql, null);

            //Populate once data received
            GridView1.DataSource = results.Tables[0];
        }

        public Task<DataSet> GetDataSetAsync (string sConnectionString, string sSQL, params SqlParameter[] parameters)
        {
            return Task.Run(() =>
            {
                using (var newConnection = new SqlConnection(sConnectionString))
                using (var mySQLAdapter = new SqlDataAdapter(sSQL, newConnection))
                {
                    mySQLAdapter.SelectCommand.CommandType = CommandType.Text;
                    if (parameters != null) mySQLAdapter.SelectCommand.Parameters.AddRange(parameters);

                    DataSet myDataSet = new DataSet();
                    mySQLAdapter.Fill(myDataSet);
                    return myDataSet;
                }
            });
        }
    }
}
This question does not have replies that have been liked.