How to use string value from SQL database to change the color of buttons?

66 Views Asked by At

I am trying to make a parking lot management system. I want to get the occupied spots from SQL database and change the button of colors related to the occupied spots (spots are saved as same names with buttons in the sql db). My buttons located in a tabcontrol and separated between 3 pages as "blue", "yellow", "purple". Blue buttons starts with "b", yellow buttons starts with "y", purple buttons starts with "p" and they all go like: b1,b2,b3,y1,y2,y3,p1,p2,p3... For example: b1 (blue page button 1) is occupied and the program will turn its color to red.

Here is the code that I have tried so far:

public static string databasePath = "parklotdata.db";
public static string connectionString = $"Data Source={databasePath};Version=3;";

public static List<string> spots_occupied = new List<string>();

static void color_occupied()
{
    admin_panel ap = new admin_panel();
    using (SQLiteConnection conn = new SQLiteConnection(connectionString))
    {
        conn.Open();
        string query = @"SELECT spot FROM parkinglot WHERE occupied=1";
        SQLiteCommand cmd = new SQLiteCommand(query, conn);
        SQLiteDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            string spot = rd[0].ToString();
            Console.WriteLine($"Spot: {spot}");
            Control[] targetcontrol = ap.tabControl1.Controls.Find(spot, true);
            if (targetcontrol != null)
            {
                targetcontrol[0].BackColor = Color.Red;
                Console.WriteLine($"Color changed for {spot}");
            }
            else
            {
                Console.WriteLine("error");
            }
        }
    }
}
0

There are 0 best solutions below