Jia Soon Loo
Jia Soon Loo

Reputation: 53

TableLayoutPanel does not display last 10 row of data

I need to create a table which will always display the last ten records of CTLog on a TableLayoutPanel. So whenever the user adds a new CTLog in Access database by clicking on a button, the table will dynamically update and display the last ten CTLogs. When adding the first ten records, I managed to get them on table but those records added after the 10th row cannot be displayed. I used the method of replacing the old labels on TableLayoutPanel by erasing the old one and then add the new ones.

private void RecentCT()
    {
        int j = 0;
        for (j = 0; j < 10; j++)
        {
            tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(j + 1, 0));
            tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(j + 1, 1));
        }

        string sql = "select Top 10 * from timer where ModelLog = @m and ShiftLog = @sl and ShiftStart = @ss and ShiftEnd = @se";

        using (OleDbCommand cmd = new OleDbCommand(sql, connection))
        {
            //all cmd.Parameters.Add actions at here
            
            try
            {
                connection.Open();
                //List<string> results = new List<string>(); I used list and foreach previously
                Label[] labels = new Label[10];
                Label[] labels2 = new Label[10];
                int i = 0;
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {                           
                             labels[i] = new Label
                            {
                                Text = reader["CTLog"].ToString(),
                                Anchor = AnchorStyles.None,
                                Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular),
                                TextAlign = ContentAlignment.MiddleCenter
                            };
                            labels2[i] = new Label
                            { 
                                Text = "Unit " + reader["UnitID"].ToString(),
                                Anchor = AnchorStyles.None,
                                Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular),
                                TextAlign = ContentAlignment.MiddleCenter
                            };
                            tableLayoutPanel1.Controls.Add(labels2[i], i + 1, 0);
                            tableLayoutPanel1.Controls.Add(labels[i], i + 1, 1);
                            i++;
                    }
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Recent cycle time records cannot be retrieved. Error: " + ex.Message);
                connection.Close();
            }

        }
    }

Did I miss out something or something is wrong in my method?

Upvotes: 0

Views: 125

Answers (1)

Jia Soon Loo
Jia Soon Loo

Reputation: 53

Problem is with the sql query I used. This is the correct sql query:

string sql = "select top 10 * from timer where ModelLog = @m and ShiftLog = @sl and ShiftStart = @ss and ShiftEnd = @se ORDER BY ID DESC";

To get the latest 10 rows of records, I must combine top and order by in desc form in a query. Because using only top keyword will only get the first 10 row, not the last ten rows.

Upvotes: 1

Related Questions