In this article we will explore how we can leverage async feature of C# 5.0 in ASP.NET. Generally to populate multiple controls in ASP.NET sync call needs to happen to database to get the data. But using async feature of C# 5.0 the data for multiple controls can be retrieved simultaneously.
Let's see how we can do this.
Step 1: Download Visual Studio Async CTP. This is supported only in VS2010.
Step 2: Add reference of AsyncCtpLibrary.dll from Documents\Microsoft Visual Studio Async CTP\Sample to your application.
Step 3: Add below namespace in the .cs file
using System.Threading; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient;
Step 4: Add a button, dropdownlist and gridview to demonstrate the perfomane benefit that can be achieved using async in C# 5.0 in ASP.NET.
< asp:Button ID="btnGetData" OnClick="btnGetData_Click" runat="server" Text="Button" /> <asp:DropDownList ID="ddlSkill" runat="server"> </asp:DropDownList> <asp:GridView ID="gvRecipie" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" ShowHeader="True"> <Columns> <asp:BoundField HeaderText="Recipie Id" DataField="RecipieId"> <ItemStyle HorizontalAlign="Center" Width="140px" /> </asp:BoundField> <asp:BoundField HeaderText="By" DataField="By"> <ItemStyle HorizontalAlign="Center" Width="140px" /> </asp:BoundField> <asp:BoundField HeaderText="Recipie Name" DataField="RecipieName"> <ItemStyle HorizontalAlign="Center" Width="140px" /> </asp:BoundField> <asp:BoundField HeaderText="Preparation Time" DataField="PreparationTime"> <ItemStyle HorizontalAlign="Center" Width="140px" /> </asp:BoundField> <asp:BoundField HeaderText="Cooking Time" DataField="CookingTime"> <ItemStyle HorizontalAlign="Center" Width="140px" /> </asp:BoundField> </Columns> <RowStyle BackColor="#EFF3FB" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>
Step 5: Fill the data on click of button.
protected void btnGetData_Click(object sender, EventArgs e) { FillData(); }
Step 6: Within FillData, FillGridView and FillDDDL will be invoked. WhenAll creates a new Task which awaits asynchronously for each task which Whenall takes in sequence. It fills the result of the tasks into an array and invokes itself on continuation.
public async void FillData() { //List of tasks to be executed before WhenAll get executes var recommendations = new List<Task<DataTable>>() { FillGridView(), FillDDDL() }; DataTable[] dt = await TaskEx.WhenAll(recommendations); gvRecipie.DataSource = dt[0]; gvRecipie.DataBind(); ddlSkill.DataSource = dt[1]; ddlSkill.DataTextField = "skillset"; ddlSkill.DataValueField = "skillid"; ddlSkill.DataBind(); }
Step 7: Below two methods FillGridView and FillDDL will be invoked asynchronously. await returns the control back immediately while the method executes.
public async Task<DataTable> FillGridView() { return await TaskEx.Run(() => { string strQuery = "Select * from Recipie"; return GetData(strQuery); }); }
public async Task<DataTable> FillDDDL() { return await TaskEx.Run(() => { string strQuery = "SELECT skillid, skillset FROM skillset"; return GetData(strQuery); }); }
Step 8: GetData method will get the data from the database.
private DataTable GetData(string strQuery) { string m_conString = "ConnectionString"; DataTable dtDept = null; using (SqlConnection con = new SqlConnection(m_conString)) { con.Open(); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strQuery, con)) { dtDept = new DataTable(); sqlAdapter.Fill(dtDept); } } return dtDept; }
This ends the article of Using Async feature of C# 5.0 in ASP.NET.
|