Home All Groups Group Topic Archive Search About

How to reference Webform textbox controls in a for next loop

Author
10 Oct 2006 2:40 PM
Kevin
ASP.NET 2.0
I have code that updates a database from a number of textboxes on a web
form.
I've had to hard coded references to my web form textboxes.  I'd like to
know how I can reference them via a for next loop.

For example, each time I give my command parameters a value I hard coded the
textbox.text reference.

     cmd.Parameters.AddWithValue("@Rank", Me.ddlQ1.Text)
     cmd.Parameters.AddWithValue("@OpenText", Me.txtQ1.Text)

I'd like to use a for next loop something like this:

for x = 1 to 10
    cmd.Parameters.AddWithValue("@Rank", Me.ddlQ" & x & ".Text)
    cmd.Parameters.AddWithValue("@OpenText", Me.txtQ" & x & ".Text)
next

What is the correct syntax for this.

Thanks in advance.

Kevin

Sample:

Private Sub SaveWork()
'This code will save all responces on this form.
Dim sSQL As String
Dim cn As New SqlConnection(Conn.ConnString)
Dim cmd As New SqlCommand(sSQL, cn)
' ------------------------------------------------------------
cmd.CommandType = System.Data.CommandType.Text
cmd.Connection = cn
cmd.Parameters.AddWithValue("@Rank", Me.ddlQ1.Text)
cmd.Parameters.AddWithValue("@OpenText", Me.txtQ1.Text)

sSQL = "UPDATE Responses"
sSQL = sSQL & " SET Rank = @Rank, "
sSQL = sSQL & " OpenText = @OpenText"
sSQL = sSQL & " WHERE ( UserID = '" & smUserID & "'"
sSQL = sSQL & " AND DocumentID = " & CInt(smDocumentID)
sSQL = sSQL & " AND QuestionNumber = 1)"

cmd.CommandText = sSQL
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()

Author
10 Oct 2006 3:00 PM
rowe_newsgroups
You could build a custom collection of the target textboxes, and use
the index (or key) to retrieve the textbox. the for next loop would be
something like this:

<pseudocode>

for x = 1 to 10
    cmd.Parameters.AddWithValue("@Rank", MyCollection(x).Text)
    cmd.Parameters.AddWithValue("@OpenText", Me.txtQ1.Text)
next x

</pseudocode>

Thanks,

Seth Rowe


Kevin wrote:
Show quoteHide quote
> ASP.NET 2.0
> I have code that updates a database from a number of textboxes on a web
> form.
> I've had to hard coded references to my web form textboxes.  I'd like to
> know how I can reference them via a for next loop.
>
> For example, each time I give my command parameters a value I hard coded the
> textbox.text reference.
>
>      cmd.Parameters.AddWithValue("@Rank", Me.ddlQ1.Text)
>      cmd.Parameters.AddWithValue("@OpenText", Me.txtQ1.Text)
>
> I'd like to use a for next loop something like this:
>
> for x = 1 to 10
>     cmd.Parameters.AddWithValue("@Rank", Me.ddlQ" & x & ".Text)
>     cmd.Parameters.AddWithValue("@OpenText", Me.txtQ" & x & ".Text)
> next
>
> What is the correct syntax for this.
>
> Thanks in advance.
>
> Kevin
>
> Sample:
>
> Private Sub SaveWork()
> 'This code will save all responces on this form.
> Dim sSQL As String
> Dim cn As New SqlConnection(Conn.ConnString)
> Dim cmd As New SqlCommand(sSQL, cn)
> ' ------------------------------------------------------------
> cmd.CommandType = System.Data.CommandType.Text
> cmd.Connection = cn
> cmd.Parameters.AddWithValue("@Rank", Me.ddlQ1.Text)
> cmd.Parameters.AddWithValue("@OpenText", Me.txtQ1.Text)
>
> sSQL = "UPDATE Responses"
> sSQL = sSQL & " SET Rank = @Rank, "
> sSQL = sSQL & " OpenText = @OpenText"
> sSQL = sSQL & " WHERE ( UserID = '" & smUserID & "'"
> sSQL = sSQL & " AND DocumentID = " & CInt(smDocumentID)
> sSQL = sSQL & " AND QuestionNumber = 1)"
>
> cmd.CommandText = sSQL
> cn.Open()
> cmd.ExecuteNonQuery()
> cn.Close()