Click here to Skip to main content
11,720,731 members (54,437 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
How can I fetch the Values from Database
and Show them in labels in windows Form Application.
I mean IF i have data in database as

Name Roll
XYZ 82

and I want to show them in form in labels
one after other.
I cant use any other Control to show data.
I just have to show the Data in labels only.
I think I have to create labels Dynamically.but how should i loop the Database and set the positions of each label.
Please assist any Idea.
Posted 7-Feb-13 23:55pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


You should have a look at the "FlowLayoutPanel" it takes care automatically of the placing based on its FlowDirection and WrapContent settings.

Creating a label is quite simple:
Dim lbl As Label
Set lbl = New Label()
Set lbl.Text = "Some text retrieved from database"

You could also have a look at the ListView component.

For the database part, I'd recommend using a DataReader. As for the query type either straight text "SELECT ColumnX FROM TableY" or a stored procedure that would return the same resultset.

Hope my comments make sense and help you solve your problem.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can iterate through each row of the database and then set the corresponding values to the labels. You can generate the labels on the run-time and can set the locations. I have shown it in the following example:

<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CodeProject
    public partial class Form1 : Form
        Label rollNo_Label;
        Label name_Label;
        public Form1()
        int y = 31;
        private void addDymanicLabels()
            for (int i = 0; i < 10; i++)
                this.rollNo_Label = new Label();
                this.rollNo_Label.Text = "Roll #: " + i;
                this.rollNo_Label.Location = new System.Drawing.Point(107, 36 + y);
                this.name_Label = new Label();
                this.name_Label.Text = "Name: "+i;
                this.name_Label.Location = new System.Drawing.Point(45, 36+y);
                y = y + 32;
        private void Form1_Load(object sender, EventArgs e)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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