ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Bind (Populate) ASP.Net GridView asynchronously (Async Method) using C#<p>Hi <a class="username" rel="EmadKhan">EmadKhan</a>,</p> <p>You need to&nbsp;ensure that the Page is marked<em>&nbsp;</em>as&nbsp;Async="true" in the page directive.</p> <p>Then set the&nbsp;Page_Load as&nbsp;async and when ever call the async task use await operator.</p> <p>Refer below link for more details.</p> <p><a href="https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45">Using Asynchronous Methods in ASP.NET 4.5</a></p> <p>Check with the below code.</p> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <pre class="brush: csharp">private string connection = &#34;Data Source=.;Initial Catalog=Northwind;Integrated Security=True;Pooling=false;&#34;; protected async void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { await GetSomeData(&#34;SELECT * FROM Employees&#34;); } } 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]; GridView1.DataBind(); } public Task&lt;DataSet&gt; GetDataSetAsync(string sConnectionString, string sSQL, params SqlParameter[] parameters) { return Task.Run(() =&gt; { using (SqlConnection newConnection = new SqlConnection(sConnectionString)) using (SqlDataAdapter 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; } }); }</pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong>&nbsp;</p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeFile=&#34;Default.aspx.cs&#34; Inherits=&#34;_Default&#34; Async=&#34;true&#34; %&gt; &lt;!DOCTYPE html&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;EmployeeID&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;FirstName&#34; HeaderText=&#34;First Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;LastName&#34; HeaderText=&#34;Last Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <table id="GridView1" border="1" rules="all" cellspacing="0"> <tbody> <tr><th scope="col">Id</th><th scope="col">First Name</th><th scope="col">Last Name</th><th scope="col">City</th><th scope="col">Country</th></tr> <tr> <td>1</td> <td>Nancy</td> <td>Davolio</td> <td>Seattle</td> <td>USA</td> </tr> <tr> <td>2</td> <td>Andrew</td> <td>Fuller</td> <td>Tacoma</td> <td>USA</td> </tr> <tr> <td>3</td> <td>Janet</td> <td>Leverling</td> <td>Kirkland</td> <td>USA</td> </tr> <tr> <td>4</td> <td>Margaret</td> <td>Peacock</td> <td>Redmond</td> <td>USA</td> </tr> <tr> <td>5</td> <td>Steven</td> <td>Buchanan</td> <td>London</td> <td>UK</td> </tr> <tr> <td>6</td> <td>Michael</td> <td>Suyama</td> <td>London</td> <td>UK</td> </tr> <tr> <td>7</td> <td>Robert</td> <td>King</td> <td>London</td> <td>UK</td> </tr> <tr> <td>8</td> <td>Laura</td> <td>Callahan</td> <td>Seattle</td> <td>USA</td> </tr> <tr> <td>9</td> <td>Anne</td> <td>Dodsworth</td> <td>London</td> <td>UK</td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/645653/Bind-Populate-ASPNet-GridView-asynchronously-Async-Method-using-C/https://www.aspforums.net:443/Threads/645653/Bind-Populate-ASPNet-GridView-asynchronously-Async-Method-using-C/Fri, 16 Aug 2019 06:33:51 GMT