This example shows how to create a custom dashboard storage to load and save dashboards in a database. To create custom dashboard storage, implement IDashboardStorage or IEditableDashboardStorage.
This example also contains an SQL file (SavedDashboards.sql). You can use it to recreate a database on your side. Update the connection string in the Web.config file to make it valid in your environment. The example uses the System.Data.SqlClient members to connect and manage an MS SQL server database.
The following API used in the example:
- LoadDashboard
Loads a dashboard with the specified ID in XDocument format from storage. - GetAvailableDashboardsInfo
Returns a list of IDs and Captions of dashboards available in the data storage. - SaveDashboard
Saves the specified dashboard with new settings to the dashboard storage. - AddDashboard
Saves a dashboard definition and its caption to the data storage and returns the ID of the new saved dashboard.
Files to Review
- DataBaseEditaleDashboardStorage.cs (VB: DataBaseEditaleDashboardStorage.vb)
- Global.asax.cs (VB: Global.asax.vb)
Documentation
More Examples
- Dashboard for ASP.NET Core - How to load and save dashboards from/to a database
- Dashboard for Web Forms - How to save dashboards created in ASPxDashboard to a DataSet
- Dashboard for Web Forms - How to load and save dashboards from/to a database
- Dashboard for MVC - How to implement multi-tenant Dashboard architecture
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
SQLSET NOCOUNT ON
GO
USE master
GO
if exists (select * from sysdatabases where name='SavedDashboards')
drop database SavedDashboards
GO
DECLARE @device_directory NVARCHAR(520)
SELECT @device_directory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1
EXECUTE (N'CREATE DATABASE SavedDashboards
ON PRIMARY (NAME = N''SavedDashboards'', FILENAME = N''' + @device_directory + N'SavedDashboards.mdf'')
LOG ON (NAME = N''SavedDashboards_log'', FILENAME = N''' + @device_directory + N'SavedDashboards.ldf'')
COLLATE SQL_Latin1_General_CP1_CI_AI')
GO
USE [SavedDashboards]
GO
/****** Object: Table [dbo].[Dashboards] Script Date: 07/06/2016 15:51:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Dashboards](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Dashboard] [varbinary](max) NULL,
[Caption] [nvarchar](255) NULL,
CONSTRAINT [PK_Dashboards] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
C#using DevExpress.DashboardWeb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;
namespace MVCDashboardDesigner {
public class DataBaseEditaleDashboardStorage : IEditableDashboardStorage {
private string connectionString;
public DataBaseEditaleDashboardStorage(string connectionString) {
this.connectionString = connectionString;
}
public string AddDashboard(XDocument document, string dashboardName) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
SqlCommand InsertCommand = new SqlCommand(
"INSERT INTO Dashboards (Dashboard, Caption) " +
"output INSERTED.ID " +
"VALUES (@Dashboard, @Caption)");
InsertCommand.Parameters.Add("Caption", SqlDbType.NVarChar).Value = dashboardName;
InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
InsertCommand.Connection = connection;
string ID = InsertCommand.ExecuteScalar().ToString();
connection.Close();
return ID;
}
}
public XDocument LoadDashboard(string dashboardID) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand GetCommand = new SqlCommand("SELECT Dashboard FROM Dashboards WHERE ID=@ID");
GetCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
GetCommand.Connection = connection;
SqlDataReader reader = GetCommand.ExecuteReader();
reader.Read();
byte[] data = reader.GetValue(0) as byte[];
MemoryStream stream = new MemoryStream(data);
connection.Close();
return XDocument.Load(stream);
}
}
public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
List<DashboardInfo> list = new List<DashboardInfo>();
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand GetCommand = new SqlCommand("SELECT ID, Caption FROM Dashboards");
GetCommand.Connection = connection;
SqlDataReader reader = GetCommand.ExecuteReader();
while (reader.Read()) {
string ID = reader.GetInt32(0).ToString();
string Caption = reader.GetString(1);
list.Add(new DashboardInfo() { ID = ID, Name = Caption });
}
connection.Close();
}
return list;
}
public void SaveDashboard(string dashboardID, XDocument document) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
SqlCommand InsertCommand = new SqlCommand(
"UPDATE Dashboards Set Dashboard = @Dashboard " +
"WHERE ID = @ID");
InsertCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
InsertCommand.Connection = connection;
InsertCommand.ExecuteNonQuery();
connection.Close();
}
}
}
}
C#using System;
using System.Configuration;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using MVCDashboardDesigner.App_Start;
namespace MVCDashboardDesigner {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
DashboardConfig.RegisterService(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
DevExpress.Web.ASPxWebControl.CallbackError += Application_Error;
}
protected void Application_Error(object sender, EventArgs e) {
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
//TODO: Handle Exception
}
}
}