Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with concatenating variable to a control name

Status
Not open for further replies.

malaygal

IS-IT--Management
Feb 22, 2006
192
US
I have 50 text boxes on my tab page(tab1) named tbx1, tbx2 etc...
I am trying to populate these text boxes with data from a datatable

For i as integer = 0 To dt.Rows.Count - 1

tab1.Controls("tbx" & (i + 1).ToString).Text = dt.Rows(i).Item("VALUES").ToString

next

I also tried

For i as integer = 0 To dt.Rows.Count - 1

Dim txt As TextBox
Dim name As String = "tbx" & (i + 1).ToString
txt = tab1.Controls.Item(name)
txt.Text = dt.Rows(i).Item("VALUES").ToString

next

I get the same error on both: Object Reference not set to an instance of an object

Any help will be greatly appreciated.
 
Are the textboxes added at design time or at run time?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Yes, the textboxes were added at design time.
 
Does it fail on the first time it tries to reference one of the textboxes? Have you stepped through the code to see where it's failing?

Also, I notice that you're using "i" as the index for the textboxes, and if there are more than 50 rows in the datatable, then the code will eventually try to reference "tbx51", which does not exist and the code will throw an error.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
It failed on the first iteration of the loop.

I tested it with:

Dim i as integer = 0
tab1.Controls("tbx" & (i + 1).ToString).Text = "some value"

and get the same error.
 

Assuming tab1 is the parent control, you may want try the .FindControl() method

Code:
Dim txt As TextBox
Dim name As String = "tbx" & (i + 1).ToString
txt = tab1.FindControl(name)
if txt isNot Nothing then 
    txt.Text = "some value"
end if


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Getting error: 'FindControl' is not a member of 'System.Windows.Forms.TabControl'.
 

Sorry, FindControl is an WebForms method. But I think you'll need to iterate through the controls collection to find the one you want, not access it directly. The Controls.Find method returns an array of all the controls with a matching name. Try this:


Code:
Dim name As String = "tbx" & (i + 1).ToString
Dim txtBoxes  = Me.Controls.Find(name, True)
If txtBoxes.Length > 0 Then
    txtBoxes(0).Text = "blah" 'from database...or something
End If


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Mark,

Worked perfectly! Thanks for all the help.
 
A Star for anyone here....?


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top