Skip to main content

Extract Data Source in ASP.NET Web Forms

  • 5 minutes to read

Extract Data Source is a compressed snapshot of data from a regular data source.

This data is saved to a local file and can be updated from the original data source at any time.

ExtractDiagram

The extract data file is optimized for data grouping. It reduces the initial dashboard load time.

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.

This topic shows how to create a data extract, add the DashboardExtractDataSource to an in-memory data source storage, and make it available to users.

Edit and Update an Extract Data Source

The DashboardExtractDataSource class allows you to request data once and save it in a 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 ConnectionOptions.DbCommandTimeout property is set to 600 to increase the query timeout.

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;
}

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

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

To update the data extract file and load the updated data in ASPxDashboard, send a request to the server and call the DashboardExtractDataSource.UpdateFile method. We recommend that you create a separate windows service that updates data automatically every hour or every day. See the following example for details:

View Example: How to Create a Data Extract in Web Forms

Register an Extract Data Source

In your application, add the .dat file to the App_Data folder. A sample data extract you can find in the following directory:

C:\Users\Public\Documents\DevExpress Demos 24.1\Components\Data

For example, your ASPX page contains the ASPxDashboard control which unique identifier is ASPxDashboardExtract:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0">
            <dx:ASPxDashboard ID="ASPxDashboardExtract" runat="server" Width="100%" Height="100%">
            </dx:ASPxDashboard>
        </div>
    </form>
</body>
</html>

You can define the Extract Data Source in the code-behind page that has the .aspx.cs or .aspx.vb extension depending on the language used:

Note

A code-behind page is one of the variants where you can register the data sources. For example, you can also register them in the Global.asax.cs (Global.asax.vb) file.

using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.Web.Hosting;

namespace WebFormsDashboardDataSources.Pages {
    public partial class ExtractDashboard : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        // ...
            ASPxDashboardExtract.SetDashboardStorage(dashboardFileStorage);

            // Uncomment the next line to allow users to create new data sources based on predefined connection strings.
            //ASPxDashboardExtract.SetConnectionStringsProvider(new DevExpress.DataAccess.Web.ConfigFileConnectionStringsProvider());

            // Create a data source storage.
            DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

            // Register an Extract data source.
            DashboardExtractDataSource extractDataSource = new DashboardExtractDataSource("Extract Data Source");
            extractDataSource.ConnectionName = "extractDataConnection";
            dataSourceStorage.RegisterDataSource("extractDataSource ", extractDataSource.SaveToXml());

            // Set the configured data source storage.
            ASPxDashboardExtract.SetDataSourceStorage(dataSourceStorage);

            ASPxDashboardExtract.ConfigureDataConnection += ASPxDashboardExtract_ConfigureDataConnection;
            ASPxDashboardExtract.InitialDashboardId = "dashboardExtract";
        }

        private void ASPxDashboardExtract_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
            if (e.ConnectionName == "extractDataConnection") {
                ExtractDataSourceConnectionParameters extractParams = new ExtractDataSourceConnectionParameters();
                extractParams.FileName = HostingEnvironment.MapPath(@"~/App_Data/SalesPersonExtract.dat");
                e.ConnectionParameters = extractParams;
            }
        }
    }
}

The Extract Data Source is now available in the Web Dashboard:

web-dashboard-ex-extract-data-source

Users can now bind dashboard items to data in the Web Dashboard’s UI.

Example 1: How to Create a Data Extract

This example shows how to create a data extract from an SQL database and update this data extract in code.

View Example: How to Create a Data Extract in Web Forms

Example 2: How to Register Data Sources

The example shows how to make a set of data sources available for users in the Web Dashboard application.

web-dashboard-a-list-of-data-sources

View Example: How to Register Data Sources for ASP.NET Web Forms Dashboard Control