Updating LinearBar Value

2 posts, 0 answers
  1. Abraham
    Abraham avatar
    5 posts
    Member since:
    Dec 2010

    Posted 21 Dec 2010 Link to this post

    Hello,

    I have used your thermometer example and now want to bind the value of the LinearBar to the latest value in a SQL Table. I'm using a service and have been successful in binding the data initially, however when a new value is inserted into the SQL Table, I cannot figure out how to have the LinearBar value update. Is this possible? Thank you

    -Abraham
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 23 Dec 2010 Link to this post

    Hello Abraham,

    When you use a service to get the latest value from a SQL Table there is no event raised when a new record is inserted.
    I think you can use a timer which will refresh the value from a service.
    The sample code below requests the service to get the Temperature property asynchronously. It uses the DispatcherTimer instance to refresh a data in 3 seconds after each request complete.
    using System;
    using System.ComponentModel;
    using System.Windows.Controls;
    using System.Windows.Threading;
    using SQLServerTemperature.ServiceReference1;
      
    namespace SQLServerTemperature
    {
        public partial class MainPage : UserControl, INotifyPropertyChanged
        {
            // WCF service client instance
            private Service1Client client = new Service1Client();
      
            private DispatcherTimer timer;
      
            // refresh interval to request WCF service
            private TimeSpan refreshInterval = TimeSpan.FromSeconds(3);
      
            private double temperature;
      
            // bound property
            public double Temperature
            {
                get
                {
                    return this.temperature;
                }
                set
                {
                    this.temperature = value;
                    OnPropertyChanged("Temperature");
                }
            }
      
            public MainPage()
            {
                InitializeComponent();
      
                this.LayoutRoot.DataContext = this;
      
                // timer initialization
                this.timer = new DispatcherTimer();
                this.timer.Interval = this.refreshInterval;
                this.timer.Tick += new EventHandler(timer_Tick);
      
                // assign request completed handler
                this.client.GetTemperatureCompleted += new EventHandler<GetTemperatureCompletedEventArgs>(client_GetTemperatureCompleted);
                // initial request
                this.client.GetTemperatureAsync();
            }
      
            void timer_Tick(object sender, EventArgs e)
            {
                this.timer.Stop();
      
                // request to the WCF service to update value
                this.client.GetTemperatureAsync();
            }
      
            void client_GetTemperatureCompleted(object sender, GetTemperatureCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    // update value
                    this.Temperature = e.Result;
                }
      
                // start timer to refresh the value
                this.timer.Start();
            }
      
            public event PropertyChangedEventHandler PropertyChanged;
      
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }

    Kind regards,
    Andrey Murzov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
Back to Top