none
How to show database table data into labels text with loop in c# windows form RRS feed

  • Question

  • hi to all, i'm facing a problem to show database data into labels. i have a database table name is EMP, EMP have 5 column, i want all EMP data into lables using loop. please see blow image to your understand.

    i write blow code for that but this code the only one row of database table, how i get all rows in labels. Please help...

     using (SQLiteConnection conn = new SQLiteConnection(Connection.Conn()))
                    {
                        string CommandText = "SELECT * FROM EMP";
                        using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
                        {
                            conn.Open();
                            SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                            DataSet ds = new DataSet();
                            sda.Fill(ds);
    
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                label1.Text = dr["EmpCode"].ToString();
                                label2.Text = dr["EmpName"].ToString();
                                label3.Text = dr["EmpCity"].ToString();
                                label4.Text = dr["EmpState"].ToString();
                            }
                         }
                      }

    Please help me any code or link will

    Saturday, June 15, 2019 4:21 PM

Answers

  • Hi John6272,

    Executing your code will only display the last piece of data. If you want to display all the data, you need to dynamically create multiple labels to store them. Just like the demo code,

        int y = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            // Column 1
            Label label1 = new Label();
            label1.Text = dr["EmpCode"].ToString();
            label1.Location = new Point(0, y);
            // Column 2
            Label label2 = new Label();
            label2.Text = dr["EmpName"].ToString();
            label2.Location = new Point(label1.Width + 10, y);
            // ...
            // Other columns
            // ...
            y += label1.Height + 5;
            this.Controls.Add(label1);
            this.Controls.Add(label2);
            // Add other labels
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 1:59 AM
    Moderator

All replies

  • Why dont you use a DataGridView or DataGrid to show your data in a table?
    Saturday, June 15, 2019 4:45 PM
  • i cant the only thing is allowed me is only label
    Saturday, June 15, 2019 4:55 PM
  • i cant the only thing is allowed me is only label
    If you don't mind me asking, why are you constrained to only labels?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, June 16, 2019 1:14 AM
    Moderator
  • Hi John6272,

    Executing your code will only display the last piece of data. If you want to display all the data, you need to dynamically create multiple labels to store them. Just like the demo code,

        int y = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            // Column 1
            Label label1 = new Label();
            label1.Text = dr["EmpCode"].ToString();
            label1.Location = new Point(0, y);
            // Column 2
            Label label2 = new Label();
            label2.Text = dr["EmpName"].ToString();
            label2.Location = new Point(label1.Width + 10, y);
            // ...
            // Other columns
            // ...
            y += label1.Height + 5;
            this.Controls.Add(label1);
            this.Controls.Add(label2);
            // Add other labels
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 1:59 AM
    Moderator