Click here to Skip to main content
Rate this: bad
good
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
ABCD 12
QWERT 75
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 8-Feb-13 0:55am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
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"
flowLayoutPanel1.Controls.Add(lbl)
 
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.
  Permalink  
v2
Rate this: bad
good
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()
        {
            InitializeComponent();
        }
 
        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.labelContainers_Panel.Controls.Add(this.rollNo_Label);
                this.rollNo_Label.Location = new System.Drawing.Point(107, 36 + y);
 
                this.name_Label = new Label();
                this.name_Label.Text = "Name: "+i;
                this.labelContainers_Panel.Controls.Add(this.name_Label);
                this.name_Label.Location = new System.Drawing.Point(45, 36+y);
 
                y = y + 32;
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            addDymanicLabels();
        }
    }
}
  Permalink  

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 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2014
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