Hi All Masters

I'm trying to create a form with multiple labels from mysql database, im confused to add newlabel text from records
here's my code

 Dim str As String
        str = " Select koderoom from room "
        da = New MySqlDataAdapter(str, conn)
        ds = New DataSet
        da.Fill(ds, "room")
        For i = 1 To ds.Tables("room").Rows.Count
            Dim newLabel As New Label
            newLabel.Name = "Label" & i
            **newLabel.Text = newLabel.Name**
            newLabel.BorderStyle = BorderStyle.FixedSingle
            newLabel.Size = New Drawing.Size(100, 100)

            If (i = 1) Then
                newLabel.Location = New Point(20, 20)
            Else
                newLabel.Location = New Point(20 * i + ((i - 1) * newLabel.Width), 20)
            End If

            Me.Controls.Add(newLabel)

        Next

When I run the above code, the label text is show " label1, label2.....
I want to dislay label text from database records
What am I missing? Any suggestions as to how to fix this?

In Advance , Thanks for the help

Imam

Ps: I'm Using Visual Studio 2010 Express

In advance, thanks for any help.

Recommended Answers

All 4 Replies

newLabel.Text = newLabel.Name

Inspite of this code at line no 9 to show data from table you can use this following codes

newLabel.Text = ds.Tables("room").Rows(i)(ColumnIndex)

or

new Label.Text=ds.Tables("room").Rows(i)("Column Name")

Hope it can help you

hi shark 1

as you suggestion i use this code

newLabel.Text = ds.Tables("room").Rows(i)("koderoom")

it show error

error.png

is any another advice for me ...??
thanks for your help
regads

@Imam_3: Sorry! I overlooked the codes for the Loop.

In vb.net any collection of objects are always 0 base indexed. So to count the total item numbers is always 1 less from the upper bound of that specific collection i.e Collection.count-1. If you have a collection of 10 objects. The index of the first element should be 0 and the index of the Last element should be Collection.Count-1 i.e. 10-1=9. So loop always rotates between 0 to Collection.Count-1
So your loop should be

For i = 0 To ds.Tables("room").Rows.Count-1

'your codes here


Next

Hope it can help you.

@shark_1

owwwhhh...i'm sorry for that ...yes it must be Rows.Count -1 ...i forgot that ..hehehe

BTW your code is OK...finnaly it's show records...
thank's for your help..

regards,

imam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.