Click here to Skip to main content
15,886,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add timer to update my data from sql and update the values at every 5 mins. So I google how to do that and saw one of the stackoverflow posts so I copy this portion
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=300000;//5 minutes
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();

private void timer1_Tick(object sender, EventArgs e)
{
     //do whatever you want 
     RefreshMyForm();
}

private void RefreshMyForm()
{

  //update form with latest Data

}


<pre lang="C#">


But I am not sure how to write in C# "update form with latest data" and I have got errors once I add in timer as described.

What I have tried:

<pre lang="C#">


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Configuration;
using System.Reflection.Emit;
using System.Dynamic;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Linq.Expressions;
using System.Data.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Drawing.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;


namespace GetThreshApp
{
    public partial class Form1 : Form
    {
        //public object DBManager { get; private set; }

        public Form1()
        {
            

            InitializeComponent();
            

           
        }

        System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        timer1.Interval=300000;//5 minutes
        timer1.Tick += new System.EventHandler(timer1_Tick);
        timer1.Start();


        private void timer1_Tick(object sender, EventArgs e)
        {
           

            string strConn = string.Empty;
            strConn = ConfigurationManager.AppSettings["DB0.DataDB"];


            //SqlConnection _con = new SqlConnection(conString);
            SqlConnection _con = new SqlConnection(strConn);

            _con.Open();

            //create command
            SqlCommand cmd = _con.CreateCommand();

            //specify stored procedure to execute
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetThreshVal";

            //execute command
            DataTable rdr = new DataTable();

            rdr.Load(cmd.ExecuteReader());

           
           Mytextbox0.Text = rdr.Rows[0][1].ToString();
           Mytextbox1.Text = rdr.Rows[1][1].ToString();
           Mytextbox2.Text = rdr.Rows[2][1].ToString();
           Mytextbox3.Text = rdr.Rows[3][1].ToString();
           Mytextbox4.Text = rdr.Rows[4][1].ToString();
           Mytextbox5.Text = rdr.Rows[5][1].ToString();
           Mytextbox6.Text = rdr.Rows[6][1].ToString();
           Mytextbox7.Text = rdr.Rows[7][1].ToString();
            
        RefreshMyForm();
        }

        private void RefreshMyForm()
        {

            //update form with latest Data

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        

            string strConn = string.Empty;
            strConn = ConfigurationManager.AppSettings["DB0.DataDB"];


            //SqlConnection _con = new SqlConnection(conString);
            SqlConnection _con = new SqlConnection(strConn);

            _con.Open();

            //create command
            SqlCommand cmd = _con.CreateCommand();

            //specify stored procedure to execute
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetThreshVal";

            //execute command
            DataTable rdr = new DataTable();


         

           
            rdr.Load(cmd.ExecuteReader());

           
                //var columnValue = rd[0];
                Mytextbox0.Text = rdr.Rows[0][1].ToString();
                Mytextbox1.Text = rdr.Rows[1][1].ToString();
                Mytextbox2.Text = rdr.Rows[2][1].ToString();
                Mytextbox3.Text = rdr.Rows[3][1].ToString();
                Mytextbox4.Text = rdr.Rows[4][1].ToString();
                Mytextbox5.Text = rdr.Rows[5][1].ToString();
                Mytextbox6.Text = rdr.Rows[6][1].ToString();
                Mytextbox7.Text = rdr.Rows[7][1].ToString();
            // Console.WriteLine(columnValue);
            //}

            
        }

    }


And once I add in timer1,it said "timer1.interval does not exist in the current context".
Posted
Updated 8-Sep-22 18:05pm

1 solution

This bit:
C#
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=300000;//5 minutes
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
Needs to be inside a method, probably the Form.Shown event handler would be best.
Your code shows it outside any method, and that is not allowed as it can't be executed!

To add an Event handler, open the Form in Design View (the display where you add controls and so forth to the Form), highlight the Form, and select "Events" in the properties window (it looks like a little lightning bolt on the Properties toolbar). Scroll down to "Shown" and double click it. That will add the handler and take you to the method to edit it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900