473,480 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to reference Webform textbox controls in a for next loop

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()


Oct 10 '06 #1
1 1891
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:
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()
Oct 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
16338
by: Hai Nguyen | last post by:
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls)...
0
901
by: Sanjay Pais | last post by:
I need to write a application in c# that would accept a web assembly with forms and web controls. It should then loop through the forms and create a list of the static objects. For web controls on...
2
8372
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
9
12879
by: Peter Afonin | last post by:
Hello: I need to clear all textboxes on the webform after the data has been submitted. How can I do this in one step? I found a C# code: // get a reference to your form control Control frm...
4
1987
by: nate axtell | last post by:
I'm looking for a way to refernce a control by a string that represents the name of the control. I dynamically create some textboxes, labels, and comboboxes each name by a loop iteration index. ...
7
7561
by: mef526 | last post by:
I would like to reference a dynamically created control and I know the name. I would like to use the following: Dim strName as String = "txtControl1" ' This is the ".Name" used when textbox was...
5
283
by: Seok Bee | last post by:
Hi All, I have a webform with a button to add record into the database. When the button is being clicked, the program will assign initial value controls in a detailsview control. When the first...
16
5121
by: mj.redfox.mj | last post by:
Can anyone help? I have a textbox which I'm programatically adding by using the following code: txtTest = New TextBox txtTest.ID = "txtLeft" + cntCount.ToString...
4
3685
by: Randy | last post by:
My situation is that I have a form on which a number of textboxes and comboboxes are added dynamically based on interaction with the user. As these controls are added, they are given names based on...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.