Click here to Skip to main content
14,384,019 members
Rate this:
Please Sign up or sign in to vote.
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);
Updated 27-Mar-19 22:53pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Set the timer Interval to 10 seconds: 10000
Handle the Timer Tick event
Start the timer.
In the tick handler, call your method.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
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:
string sConStr = @"your_connection_string_here!";
DataTable dt = new DataTable();

//1. create SqlConnection
using(SqlConnection connection  = new SqlConnection(sConStr))
    //open connection
    //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

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[^]

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100