XPO Data Source in ASP.NET Web Forms
- 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 24.2\Components\Data directory.
In Web.config, specify a connection string to this SQLite database:
<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:
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); }
}
}
}
Register an XPO Data Source
For example, your ASPX page contains the ASPxDashboard
control which unique identifier is ASPxDashboardXpo
:
<!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:
- Create a DashboardXpoDataSource instance.
- Pass the persistent object‘s type to the XPObjectSource.SetEntityType method.
- Register the created data source instance in the data source storage.
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;
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";
}
}
}
The XPO Data Source is now available in the Web Dashboard:
Users can now 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.