How to: Show Live Data from a Database (using CustomCallback)

  • 4 minutes to read

You can show live data in gauges. This example uses a linear gauge to show the maximum unit price (UnitPrice) of products from the Northwind database. A timer updates the gauge every two seconds to display real-time prices.

View Example

using System;
using DevExpress.Web.ASPxGauges.Gauges.Linear;
using DevExpress.Web.ASPxGauges;
using System.Data;
using System.Web.UI;

namespace WebApplication1 {
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Init(object sender, EventArgs e) {
            if (!IsPostBack && !IsCallback) {
                UpdateGauge();
            }
        }
        private void SetupDataSourceInternal() {
            SqlDataSource1.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
            SqlDataSource1.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataSet;
        }
        private void UpdateGauge() {
            SetupDataSourceInternal();
            UpdateScaleInternal(gauge);
        }
        private void UpdateScaleInternal(ASPxGaugeControl gauge) {
            float oldValue = ((LinearGauge)gauge.Gauges[0]).Scales[0].Value;
            //use a random value, just for demonstration purposes.
            //DataView dv = SqlDataSource1.Select(DataSourceSelectArguments.Empty) as DataView;
            //float newValue = Convert.ToSingle(dv.Table.Rows[0][0]);
            float newValue = new Random().Next(300);
            if (oldValue != newValue) {
                ((LinearGauge)gauge.Gauges[0]).Scales[0].Value = newValue;
                gauge.DataBind();
            }
        }
        protected void gauge_CustomCallback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            UpdateGauge();
        }
    }
}