Dear All,
I have a time ticker which updates the time say now for 5 seconds. Then it keep calling a function and in it via sql query based on the current time it shows the relevent sum data. I got few things to confirm am I doing the right method to refresh the chart and secondly I would like to continously show updates on my data based on the live data that is being inserted into my db.

public partial class Form1 : Form
        static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();            
        static int alarmCounter = 1;
        static bool exitFlag = false;    
        // This is the method to run when the timer is raised.
        private  void TimerEventProcessor(Object myObject,
                                                EventArgs myEventArgs)            {

            MySqlConnection localConnection1;
            MySqlCommand command;    
            localConnection1 = new MySqlConnection("Address='******';Database='****';User Name='****';Password='*****';Pooling='false'");
            MySqlDataAdapter myLocalDataAdapter1 = new MySqlDataAdapter();    
            String mySelectQuery1 = "Select  " +
                                    "sum(pc) as sumPC" +
                                    "timeStampID " +
                                    "From byteTransmit Where timeStampID='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'";    
            MySqlCommand myCommand1 = new MySqlCommand(mySelectQuery1, localConnection1);
            DataTable myDataTable1 = new DataTable();
            myLocalDataAdapter1.SelectCommand = myCommand1;
            chart1.DataSource = myDataTable1;    
            chart1.Series["SE"].XValueMember = "timeStampID";    
            chart1.Series["SE"].YValueMembers = "sumPC";              
        public Form1()
        private void Form1_Load(object sender, EventArgs e)
            myTimer.Tick += new EventHandler(TimerEventProcessor);    
            // Sets the timer interval to 5 seconds.
            myTimer.Interval = 5000;

I wouldn't reconnect to the database every timer interval. I would connect to it once then poll it for new data each time and disconnect from it when you don't need the updates anymore. This will eliminate all the overhead of establishing a connection with a database.

Also, if you keep track of the timestamp of the most recent data you can poll for everything after it instead of using DateTime.Now. This will allow you to get all of the data (not just the data that so happens to fall on your timer interval) which can then be added to the graph. Also, you shouldn't need to re-set the databindings on each interval, either. All of your members seem to be static, I think they should be non-static.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.