Example T506198
Visible to All Users

Dashboard for Web Forms - How to Work with a Data Extract

This example demonstrates basic approaches and code snippets that can be used to bind the ASPxDashboard control to the DashboardExtractDataSource.

The Extract Data Source improves performance when a complex query or a stored procedure takes a significant amount of time to get data from a database.

The DashboardExtractDataSource class implements the Extract Data Source concept, and allows you to request data once and save it in the compressed and optimized form to a file. Subsequently, an application can retrieve data from that file or create a new file when data is updated.

The code snippet below creates the DashboardExtractDataSource and connects it to the DashboardSqlDataSource instance. The DbCommandTimeout property is set to 600 to increase the query timeout.

C#
DashboardSqlDataSource nwindDataSource = new DashboardSqlDataSource("Northwind Invoices", "nwindConnection"); SelectQuery invoicesQuery = SelectQueryFluentBuilder .AddTable("Invoices") .SelectColumns("City", "Country", "Salesperson", "OrderDate", "Shippers.CompanyName", "ProductName", "UnitPrice", "Quantity", "Discount", "ExtendedPrice", "Freight") .Build("Invoices"); nwindDataSource.Queries.Add(invoicesQuery); nwindDataSource.ConnectionOptions.DbCommandTimeout = 600; DashboardExtractDataSource extractDataSource = new DashboardExtractDataSource("Invoices Extract Data Source"); extractDataSource.ExtractSourceOptions.DataSource = nwindDataSource; extractDataSource.ExtractSourceOptions.DataMember = "Invoices"; extractDataSource.FileName = extractFileName;

To set the correct extract file name for loaded dashboards, handle the ASPxDashboard.ConfigureDataConnection event:

C#
protected void ASPxDashboard1_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) { ExtractDataSourceConnectionParameters extractCP = e.ConnectionParameters as ExtractDataSourceConnectionParameters; if (extractCP != null) { extractCP.FileName = extractFileName; } }

To add the data source to the ASPxDashboard control use the solution described in the Register Default Data Sources help topic:

C#
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage(); dataSourceStorage.RegisterDataSource("extractDataSource", GetExtractDataSource().SaveToXml()); ASPxDashboard1.SetDataSourceStorage(dataSourceStorage);

To create a data extract file when the dashboard is loaded for the first time, use the following code:

C#
if (!File.Exists(extractFileName)) { using (var ds = CreateExtractDataSource()) { ds.UpdateExtractFile(); } }

To update the data extract file and load the updated data in ASPxDashboard, send an AJAX request to the server and call the DashboardExtractDataSource.UpdateFile method there. Once a new file is created on the server, you can return the callback back to the client and call the ASPxClientDashboard.ReloadData method to reload the control with new data:

JavaScript
function UpdateExtractDataSource() { $.ajax({ url: "Default.aspx/UpdateExtractDataSource", type: "POST", data: {}, contentType: "application/json; charset=utf-8" }).then(function (result) { dashboard.ReloadData(); DevExpress.ui.notify(result.d, "success", 5000); }, function () { DevExpress.ui.notify("We could not update extract data source.", "error", 2000) }); }
C#
[WebMethod] public static string UpdateExtractDataSource() { DashboardExtractDataSource ds = CreateExtractDataSource(); StringBuilder sb = new StringBuilder("We updated your extract data source. "); var task = DashboardExtractDataSource.UpdateFile(ds, (fileName, result) => { sb.AppendLine($"{DateTime.Now.ToString("T")} - Data Updated - {result} - {Path.GetFileName(fileName)}. "); }, (fileName, result) => { sb.AppendLine($"{DateTime.Now.ToString("T")} - File Updated - {result} - {Path.GetFileName(fileName)}. "); }); // Wait until the data is refreshed in the Extract Data Source. task.Wait(); return sb.ToString(); }

In this example, click a button to extract data. However, in a real-life application, this solution can be insufficient (for example, the site may be deployed to the web farm server). We recommend that you create a separate windows service that updates data automatically every hour or every day.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

ASP_WebDashboard/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP_WebDashboard.Default" %> <%@ Register assembly="DevExpress.Dashboard.v24.2.Web.WebForms, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.DashboardWeb" tagprefix="dx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function UpdateExtractDataSource() { $.ajax({ url: "Default.aspx/UpdateExtractDataSource", type: "POST", data: {}, contentType: "application/json; charset=utf-8" }).then(function (result) { dashboard.ReloadData(); DevExpress.ui.notify(result.d, "success", 5000); }, function () { DevExpress.ui.notify("We could not update extract data source.", "error", 2000) }); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" value="Update Extract Data Source" onclick="UpdateExtractDataSource();" /> <dx:ASPxDashboard ID="ASPxDashboard1" runat="server" ClientInstanceName="dashboard" DashboardStorageFolder="~/App_Data/Dashboards/" OnConfigureDataConnection="ASPxDashboard1_ConfigureDataConnection" > </dx:ASPxDashboard> </div> </form> </body> </html>
ASP_WebDashboard/Default.aspx.cs(vb)
C#
using DevExpress.DashboardCommon; using DevExpress.DashboardWeb; using DevExpress.DataAccess.Sql; using System; using System.IO; using System.Text; using System.Threading; using System.Web.Hosting; using System.Web.Services; namespace ASP_WebDashboard { public partial class Default : System.Web.UI.Page { static string extractFileName = HostingEnvironment.MapPath("~/App_Data/ExtractDataSource/") + "ExtractDS.dat"; protected void Page_Load(object sender, EventArgs e) { DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage(); dataSourceStorage.RegisterDataSource("extractDataSource", CreateExtractDataSource().SaveToXml()); ASPxDashboard1.SetDataSourceStorage(dataSourceStorage); if (!File.Exists(extractFileName)) { using (var ds = CreateExtractDataSource()) { ds.UpdateExtractFile(); } } } protected void ASPxDashboard1_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) { ExtractDataSourceConnectionParameters extractCP = e.ConnectionParameters as ExtractDataSourceConnectionParameters; if (extractCP != null) { extractCP.FileName = extractFileName; } } private static DashboardExtractDataSource CreateExtractDataSource() { DashboardSqlDataSource nwindDataSource = new DashboardSqlDataSource("Northwind Invoices", "nwindConnection"); SelectQuery invoicesQuery = SelectQueryFluentBuilder .AddTable("Invoices") .SelectColumns("City", "Country", "Salesperson", "OrderDate", "Shippers.CompanyName", "ProductName", "UnitPrice", "Quantity", "Discount", "ExtendedPrice", "Freight") .Build("Invoices"); nwindDataSource.Queries.Add(invoicesQuery); nwindDataSource.ConnectionOptions.DbCommandTimeout = 600; DashboardExtractDataSource extractDataSource = new DashboardExtractDataSource("Invoices Extract Data Source"); extractDataSource.ExtractSourceOptions.DataSource = nwindDataSource; extractDataSource.ExtractSourceOptions.DataMember = "Invoices"; extractDataSource.FileName = extractFileName; return extractDataSource; } [WebMethod] public static string UpdateExtractDataSource() { DashboardExtractDataSource ds = CreateExtractDataSource(); StringBuilder sb = new StringBuilder("We updated your extract data source. "); var task = DashboardExtractDataSource.UpdateFile(ds, (fileName, result) => { sb.AppendLine($"{DateTime.Now.ToString("T")} - Data Updated - {result} - {Path.GetFileName(fileName)}. "); }, (fileName, result) => { sb.AppendLine($"{DateTime.Now.ToString("T")} - File Updated - {result} - {Path.GetFileName(fileName)}. "); }); // Wait until the data is refreshed in the Extract Data Source. task.Wait(); return sb.ToString(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.