Back to Devexpress

Extract Data Source in ASP.NET Web Forms

dashboard-401408-web-dashboard-integrate-dashboard-component-aspnet-web-forms-dashboard-control-register-default-data-sources-extract-data-source.md

latest11.1 KB
Original Source

Extract Data Source in ASP.NET Web Forms

  • Jun 02, 2023
  • 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.

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.

cs
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;
}
vb
Private Shared Function CreateExtractDataSource() As DashboardExtractDataSource
    Dim nwindDataSource As New DashboardSqlDataSource("Northwind Invoices", "nwindConnection")
    Dim invoicesQuery As SelectQuery = 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

    Dim extractDataSource As New DashboardExtractDataSource("Invoices Extract Data Source")
    extractDataSource.ExtractSourceOptions.DataSource = nwindDataSource
    extractDataSource.ExtractSourceOptions.DataMember = "Invoices"
    extractDataSource.FileName = extractFileName

    Return extractDataSource
End Function

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

cs
if (!File.Exists(extractFileName)) {
    using (var ds = CreateExtractDataSource()) {
        ds.UpdateExtractFile();
    }
}
vb
If Not File.Exists(extractFileName) Then
    Using ds = CreateExtractDataSource()
        ds.UpdateExtractFile()
    End Using
End If

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 25.2\Components\Data

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

aspx
<!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.

cs
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;
            }
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System
Imports System.Web.Hosting

Namespace WebFormsDashboardDataSources.Pages

    Public Partial Class ExtractDashboard
        Inherits Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' ...
            ' Create a data source storage.
            Dim dataSourceStorage As DataSourceInMemoryStorage = New DataSourceInMemoryStorage()
            ' Register an Extract data source.
            Dim extractDataSource As DashboardExtractDataSource = New DashboardExtractDataSource("Extract Data Source")
            extractDataSource.ConnectionName = "extractDataConnection"
            dataSourceStorage.RegisterDataSource("extractDataSource ", extractDataSource.SaveToXml())
            ' Set the configured data source storage.
            ASPxDashboardExtract.SetDataSourceStorage(dataSourceStorage)
            AddHandler ASPxDashboardExtract.ConfigureDataConnection, AddressOf Me.ASPxDashboardExtract_ConfigureDataConnection
            ASPxDashboardExtract.InitialDashboardId = "dashboardExtract"
        End Sub

        Private Sub ASPxDashboardExtract_ConfigureDataConnection(ByVal sender As Object, ByVal e As ConfigureDataConnectionWebEventArgs)
            If Equals(e.ConnectionName, "extractDataConnection") Then
                Dim extractParams As ExtractDataSourceConnectionParameters = New ExtractDataSourceConnectionParameters()
                extractParams.FileName = HostingEnvironment.MapPath("~/App_Data/SalesPersonExtract.dat")
                e.ConnectionParameters = extractParams
            End If
        End Sub
    End Class
End Namespace

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

Users can 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.

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