loop thru labels and set value

I have a series of 12 labels on my form and I want to use a for loop to go thru the list and set a value from a db call.
this is my function:
Private Sub GetQuestions(ByVal SectionID As Integer)
        Dim myDR As SqlDataReader
        Dim myQuestion As New Utilities
        Dim i As Integer
        myDR = myQuestion.GetQuestions(SectionID)
        While myDR.Read
            For i = 1 To 12
                If Not IsDBNull(myDR("QuestionDesc")) Then

                    CType(lblQuestion & i, Label).Text = myDR("QuestionDesc")
                End If
            Next
        End While
    End Sub

I do not know how to concatenate the i to the end of the name of the label


thanks

Jcook32
jcook32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
For each ctl as Control in Me.Controls
  If typeof ctl is Label then
    if ctl.Name.StartsWith ("lblQuestion") then
       ctl.Text =Convert.ToString(  myDr!QuestionDesc)
  end if
end if

Next

Something like this?

You have to change your myDR.Read (it will progress each time you call the read, now it will read, update 12 labels, read again, update 12 labels...etc )
0
Bob LearnedCommented:
Cool way:

Imports System.Reflection

Public Class ApplicationControls

  Public Shared Function FindControl(ByVal owner As Form, ByVal name As String) As Control

    Dim propInfo As PropertyInfo = _
     owner.GetType().GetProperty(name, BindingFlags.IgnoreCase Or _
     BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public)

    If Not propInfo Is Nothing Then
      Dim value As Object = propInfo.GetValue(owner, Nothing)
      If TypeOf value Is Control Then
        Return value
      End If
    Else
      Return Nothing
    End If

  End Function  'FindControl

End Class

Sample usage:
  CType(ApplicationControls.FindControl(Me, "lblQuestion" & i).Text = myDR("QuestionDesc").ToString()

Bob
0
PockyMasterCommented:
The setdown on your solution is bob, that controlsnames don't need to be unique (if you create them by code that is). So you will not neccessarily find your control unless you return array instead of single item.
But reflection is cool, and costly, but still cool :D
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Bob LearnedCommented:
@PockyMaster:
>>unless you return array instead of single item
Huh?  You can't see the confusion on my face, can you?

Bob
0
jcook32Author Commented:
TheLearnedOne,
     The type Form is invalid in the function call. i tried. is there another type?

thanks
i like the reflection
0
Bob LearnedCommented:
System.Windows.Forms.Form

Add this line to the top:

    Imports System.Windows.Forms


Bob
0
jcook32Author Commented:
this is a asp.net/vb.net app. not a windows form app.
0
Bob LearnedCommented:
Geez, why didn't I ask or why didn't you say so ;)

Throw out all that code, and use this (FindControl is already defined for web pages):

  CType(FindControl("lblQuestion" & i), Label).Text = myDR("QuestionDesc").ToString()

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PockyMasterCommented:
TheLearnedOne:

Try to do this:

Dim lbl as label

for i as integer = 0 to 10
lbl = new Label
lbl.Name = "whatever"
myform.Controls.Add(lbl)
next


Now you have 11 labels with name "whatever" :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.