Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem
I need to execute function retrieve image every 10 second by using timer control

i already put timer control on windows form but how to run and execute this function

in timer every 10 second .

meaning i need every 10 second refresh and executing function below

I work on windows form csharp visual studio 2017 with sql server 2012

What I have tried:

public void GetMembersData()
      {
           string sqlGetLast = @"select isnull((select top 1 MemberCode from members order by MemberCode desc),1) ";
           int MemberCode = Utilities.ObjectConverter.ConvertToInteger(DataAccess.ExecuteScalar(sqlGetLast));
           string sqlImage = "select MemberImage FROM Members WHERE MemberCode =" + MemberCode + "";
           if (Utilities.ObjectConverter.ConvertToString(DataAccess.ExecuteScalar(sqlImage)) != "")
           {
               byte[] MemberImage = (byte[])DataAccess.ExecuteScalar(sqlImage);
               pictureBox1.Image = ByteArrayToImagebyMemoryStream(MemberImage);
           }
       }
Posted
Updated 27-Mar-19 21:53pm

Set the timer Interval to 10 seconds: 10000
Handle the Timer Tick event
Start the timer.
In the tick handler, call your method.
 
Share this answer
 
Solution #1 by OriginalGriff[^] is excellent and explains in details how to use timer.

I wanted to move your focus to these things:
1) possible Sql Injection[^]
Never use commands like this:
C#
string sqlImage = "select MemberImage FROM Members WHERE MemberCode =" + MemberCode + "";

Instead of this, use parameterized queries! See: How to: Execute a Parameterized Query | Microsoft Docs[^]
For example:
C#
string sConStr = @"your_connection_string_here!";
DataTable dt = new DataTable();

//1. create SqlConnection
using(SqlConnection connection  = new SqlConnection(sConStr))
{
    //open connection
    connection.Open()
    //set command text
    string sql = @"SELECT * FROM TableName WHERE TextField Like @SomeString;";
    //2. create command
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@SomeString", "%whatever%")
        //3. create reader
        using (SqlDataReader reader = command.ExecuteReader())
        {
            //4. load data into datatable
            dt.Load(reader)
        }
    }
}


2) unnecessary database calls
I'm pretty sure that you want to check if MemberImage has been changed. If i'm right, i'd suggest to use SqlDependency Class (System.Data.SqlClient) | Microsoft Docs[^]

For further details, please see:
Detecting Changes with SqlDependency | Microsoft Docs[^]
Using SqlDependency for data change events[^]
Query Notification using SqlDependency and SqlCacheDependency[^]
 
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