for loop is used to assign values to textboxed?

G

Guest

I try to use "for" loop to retrieve and assign values in web form. The code
is in the following. But it can not be compiled.
What I want to do is:
txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
.....
txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")

------code -------
Dim field As String = "Q"
Dim score As String = "txtQ"
For i As Integer = 1 To 10
field += i
score += i
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Next
 
G

Guest

you cannot have a Textbox and a local variable both called 'score'. If this
is not the problem what is the compliation error?
 
G

Guest

textbox IDs:
txtQ1, txtQ2, ...., txtQ10.

Can I use a variable and FOR loop to assign values to those boxes like what
I did?
The proble is the variable score as string. I have changed as follows
-----
For i As Integer = 1 To 35
field = "Q" + i
score = "txtQ" + i
If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field)) Then
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Else
score.Text = "NA"
End If

Next
-----
It is suppoesed to do the following:

i=1, score = txtQ1 and field=Q1
i=2, score =txtQ2 and field =Q2
....
i=10, score =txtQ10 and field=Q10

But they do not know score ( think it undefined).
How to use loop to get the result?

David
 
G

Guest

What is score defined as? In one part of your code you are assigning it as
though it is a string variable.
e.g.
score = "txtQ" + i
elsewhere it seems you are trying to assign to a Text property of score,
which would suggest score is a control.
e.g.
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)

again later in the example you are assigning score to the control txtQ1 not
the test property if txtQ1
e.g.
i=1, score = txtQ1 and field=Q1

if you assign score to txtQ1 then you are assigning score to the control not
the text within.
e.g.
i=1, score = txtQ1 and field=Q1
should be
i=1, score = txtQ1.Text

if you assign score to "txtQ" + i this will give score a text value of
"txtQ1" .... "txtQ35" not the text value of the control.


A string variable should alsway be assigned <variable> = <string value>
A textbox Text property should be assigned <control>.Text = <string value>


hope this helps if not can you post the entire method with the declaration
for 'score.
 
G

Guest

Sorry, I just come back from meeting.
I mean that use score="txtQ"+i+".Text" instead. I will try it.

David
 
G

Guest

I try to use "score" as variable of textbox control and change the value in
loop.
So you mean that I have to declare score as textbox:
dim score as textbox

David
 
G

Guest

If I declare score as TextBox, how can I assign it txtQ1.Text, txtQ2.Text,
..... and so on dynamically? where TxtQ1, txtQ2, ... are TextBox control's IDs.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top