By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

for loop is used to assign values to textboxed?

P: n/a
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
--------------

Thank you for your help.

David
Apr 4 '06 #1
Share this Question
Share on Google+
6 Replies


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

"david" wrote:
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
--------------

Thank you for your help.

David

Apr 4 '06 #2

P: n/a
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

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

"david" wrote:
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
--------------

Thank you for your help.

David

Apr 4 '06 #3

P: n/a
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.

"david" wrote:
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

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

"david" wrote:
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
--------------

Thank you for your help.

David

Apr 4 '06 #4

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

David

"Steven" wrote:
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.

"david" wrote:
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

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

"david" wrote:

> 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
> --------------
>
> Thank you for your help.
>
> David

Apr 4 '06 #5

P: n/a
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

"Steven" wrote:
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.

"david" wrote:
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

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

"david" wrote:

> 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
> --------------
>
> Thank you for your help.
>
> David

Apr 4 '06 #6

P: n/a
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

"david" wrote:
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

"Steven" wrote:
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.

"david" wrote:
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

"Steven" wrote:

> you cannot have a Textbox and a local variable both called 'score'. If this
> is not the problem what is the compliation error?
>
> "david" wrote:
>
> > 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
> > --------------
> >
> > Thank you for your help.
> >
> > David

Apr 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.