Click here to Skip to main content
15,868,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have sequentially numbered Labels and TextBoxes named e.g. txtAmount1 - txtAmount9 and I need to set the .Text value from a database. I can create a DataConnector and from this fill a DataTable then iterate in a foreach procedure but how do I refer to the required textboxes?

What I have tried:

My code so far is:
DataTable dtFields = dcFields.DataSelect("SELECT [Sub-Type] FROM tblTransportSubType WHERE Type = '"+ bu +"'");
int co = 1;
foreach (DataRow row in dtFields.Rows)
{
lblType1.Text = row["Sub-Type"].ToString();
co ++;
}
Posted
Updated 22-Sep-19 4:49am
Comments
Richard Deeming 20-Sep-19 9:36am    
"SELECT [Sub-Type] FROM tblTransportSubType WHERE Type = '"+ bu +"'"

Don't do it like that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
BillWoodruff 22-Sep-19 9:56am    
WPF ? ASP ?

Like this:
C#
for(int i = 1; i<=9; i++)
{
    TextBox tb = (TextBox) this.Controls[string.Format("TxtBox{0}", i)];
    tb.Text = i.ToString();
}


Note: If textboxes are placed on another control (i.e.: panel), you have to refer to this control, which holds all textboxes.

For further details, please see:
Control.Controls Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
BillWoodruff 22-Sep-19 10:53am    
Hi, there's an error in the way you are using 'Format.
Maciej Los 24-Sep-19 6:18am    
Thank you, Bill.
Corrected!
You can use FindControl to programatically find controls by ID

for(int i = 1; i <=3; i++)
{
    TextBox txtAmount = (TextBox) FindControl("txtAmount" + i.ToString());

    System.Diagnostics.Debug.WriteLine(txtAmount.Text);
}


You have to ensure you execute FindControl on the relevant container. We don't know where your controls are housed so we can't give specific advice, but the following page gives examples of how to use FindControl on master pages, child pages, grid views etc

Finding controls | The ASP.NET Forums[^]
 
Share this answer
 
If this is WinForms, you can use the 'Find operator of any ControlCollection to find all instances of Controls with the specified 'Name key. The 'Find operator has a 'SearchAllChildren optional parameter that will recursively work its way through ContainerControls inside its target ControlCollection.

For WPF, look at use of 'FindName with 'VisualTreehelper [^]

However, it's best to not use recursion unless absolutely necessary ... better you know the ContainerControl that actually contains your Labels, Buttons, etc.

Ideally, you'd databind your collection of Labels/TextBoxes so data updates should be seamless: we'd need to know much more about your db and the app/pages structure to pursue that.

If I couldn't use databinding, I might do something like this:
Dictionary<int, Label> IndexToLabel = new Dictionary<int, Label>
{
    {0, label1 }, {1, label2} // and so on
};
Essentially hard-coding quick access to the Labels.
 
Share this answer
 
v3
Comments
Richard Deeming 23-Sep-19 10:37am    
Since the numbers are sequential, a List<Label> might be a better choice than a dictionary. :)
BillWoodruff 23-Sep-19 11:50am    
Hi Richard, I thought about that, but decided I wanted the indexes "baked in," rather than dependent on ordinality. thanks, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900