Back to Devexpress

XPO Data Source in ASP.NET Web Forms

dashboard-401412-web-dashboard-integrate-dashboard-component-aspnet-web-forms-dashboard-control-register-default-data-sources-xpo-data-source.md

latest7.4 KB
Original Source

XPO Data Source in ASP.NET Web Forms

  • Sep 20, 2024
  • 3 minutes to read

This topic shows how to add the DashboardXpoDataSource to an in-memory data source storage, and make it available to users. This example uses XPO Business Model based on the SQLite database.

Create an XPO Model

In your application, add the SQLite database to the project. In this example, it is nwind.db from the C:\Users\Public\Documents\DevExpress Demos 25.2\Components\Data directory.

In Web.config , specify a connection string to this SQLite database:

xml
<configuration>
  <connectionStrings>
    <add name="NWindConnectionStringSQLite" connectionString="XpoProvider=SQLite;Data Source=|DataDirectory|\nwind.db" />
  </connectionStrings>
</configuration>

Create an XPO model based on the SQLite nwind.db database:

cs
using DevExpress.Xpo;

namespace WebFormsDashboardDataSources {
    [Persistent("Categories"), DeferredDeletion(false)]
    public class Category: XPCustomObject {
        int categoryId;
        string categoryName;
        string description;
        [Key]
        public int CategoryID {
            get { return categoryId; }
            set { SetPropertyValue<int>("CategoryID", ref categoryId, value); }
        }
        public string CategoryName {
            get { return categoryName; }
            set { SetPropertyValue<string>("CategoryName", ref categoryName, value); }
        }
        public string Description {
            get { return description; }
            set { SetPropertyValue<string>("Description", ref description, value); }
        }
    }
}
vb
Imports DevExpress.Xpo

Namespace WebFormsDashboardDataSources

    <Persistent("Categories"), DeferredDeletion(False)>
    Public Class Category
        Inherits XPCustomObject

        Private categoryIdField As Integer

        Private categoryNameField As String

        Private descriptionField As String

        <Key>
        Public Property CategoryID As Integer
            Get
                Return categoryIdField
            End Get

            Set(ByVal value As Integer)
                SetPropertyValue(Of Integer)("CategoryID", categoryIdField, value)
            End Set
        End Property

        Public Property CategoryName As String
            Get
                Return categoryNameField
            End Get

            Set(ByVal value As String)
                SetPropertyValue(Of String)("CategoryName", categoryNameField, value)
            End Set
        End Property

        Public Property Description As String
            Get
                Return descriptionField
            End Get

            Set(ByVal value As String)
                SetPropertyValue(Of String)("Description", descriptionField, value)
            End Set
        End Property
    End Class
End Namespace

Register an XPO Data Source

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

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="ASPxDashboardXpo" runat="server" Width="100%" Height="100%">
            </dx:ASPxDashboard>
        </div>
    </form>
</body>
</html>

You can define the XPO 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;

namespace WebFormsDashboardDataSources.Pages {
    public partial class XpoDashboard : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        // ...
            // Create a data source storage.
            DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

            // Register an XPO data source.
            DashboardXpoDataSource xpoDataSource = new DashboardXpoDataSource("XPO Data Source");
            xpoDataSource.ConnectionStringName = "NWindConnectionStringSQLite";
            xpoDataSource.SetEntityType(typeof(Category));
            dataSourceStorage.RegisterDataSource("xpoDataSource", xpoDataSource.SaveToXml());

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

            ASPxDashboardXpo.InitialDashboardId = "dashboardXpo";
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System

Namespace WebFormsDashboardDataSources.Pages

    Public Partial Class XpoDashboard
        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 XPO data source.
            Dim xpoDataSource As DashboardXpoDataSource = New DashboardXpoDataSource("XPO Data Source")
            xpoDataSource.ConnectionStringName = "NWindConnectionStringSQLite"
            xpoDataSource.SetEntityType(GetType(Category))
            dataSourceStorage.RegisterDataSource("xpoDataSource", xpoDataSource.SaveToXml())
            ' Set the configured data source storage.
            ASPxDashboardXpo.SetDataSourceStorage(dataSourceStorage)
            ASPxDashboardXpo.InitialDashboardId = "dashboardXpo"
        End Sub
    End Class
End Namespace

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

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

Example

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