×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Problem with concatenating variable to a control name

Problem with concatenating variable to a control name

Problem with concatenating variable to a control name

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

RE: Problem with concatenating variable to a control name

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!

RE: Problem with concatenating variable to a control name

(OP)
Yes, the textboxes were added at design time.

RE: Problem with concatenating variable to a control name

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!

RE: Problem with concatenating variable to a control name

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

RE: Problem with concatenating variable to a control name


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

RE: Problem with concatenating variable to a control name

(OP)
Getting error: 'FindControl' is not a member of 'System.Windows.Forms.TabControl'.

RE: Problem with concatenating variable to a control name


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

RE: Problem with concatenating variable to a control name

(OP)
Mark,

Worked perfectly! Thanks for all the help.

RE: Problem with concatenating variable to a control name

A Star for anyone here....?


---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close