How to create dynamic textboxes in a table from sql database c# asp.net

I am pulling results from a sql database  (server names and hard drive space info) using c# and a datareader in visual studio web express and want to display them in textboxes created dynamically in a  table.  In the past I have spent a lot of time and effort making the table with all the textboxes where the sqldatabase info currently goes into and currently it all works well.  The only problem is everything is hard coded with a  lot of code specifiying which server info goes into which text box and these textboxes are set to each server name.  So the problem comes when I want to remove a server name from the database or add one in.  The code will not automatically adapt to this situation and add or remove textboxes as the textboxes are named specfiically for each server.  I then have to go into the code and add more code (or remove code) and rename textboxes etc to adapt to the servername change.

Ideally, I would like a tight bit of code that loops around the server info gained from the database and dynamically creates the textboxes and places them in my table.     What would the methology of this be if it is even possible? An array of objects?  I tried using a datagrid for this problem but I found it to be too simple as the content i wanted to display was difficult to imlpement,  eg I  wanted to include a listbox with all the hard drive info listed. When i just hard coded it into my own premade table and prenamed listbox it was easy.

It would be great if you could do something in C# like      

while (!servername)  // while the datareader is not empty
{
servername.textbox.text = "some info"; // create a textbox on the fly with the servername
}

Any suggestions for combating this ?

Thanks in advance.

As you can see from my code below there are a lot of prenamed textboxes and listboxes which I would like to create dynamically instead of having loads of code!

switch (i)
                    {
                        case "dublfs01": //add server hd space to correct listbox
                            dublin1ListBox.Items.Add(ndivSize.ToString("#0.00"));
                            dublinImage2.Visible = true;
                            //display hd size in label
                            totalSize += ndivSize;
                            if (((((totalSize - totalSize2[0]) / totalSize) * 100) > -5) && ((((totalSize - totalSize2[0]) / totalSize) * 100) < 5))
                            {
                                dublin1Label.ForeColor = Color.Black; dublin1perLabel.ForeColor = Color.Black;
                                dublin1perLabel.Text = string.Format((((totalSize - totalSize2[0]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublin1Label.Text = string.Format( (totalSize - totalSize2[0]).ToString("#0.0") + "{1}", "+", " GB");
                            }
 
                            else
                            {
                                dublin1Label.ForeColor = Color.Red; dublin1perLabel.ForeColor = Color.Red;
                                dublin1perLabel.Text = string.Format((((totalSize - totalSize2[0]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublin1Label.Text = string.Format((totalSize - totalSize2[0]).ToString("#0.0") + "{1}", "-", " GB");
                            }
 
                            //this if else checks to see that 7 days ago was drive space total above or below 5% then displays
                            // in black or red text accordingly along with the percentage of change
                            if (((((totalSize - Days7AgoResultsStore[0]) / totalSize) * 100) > -10) && ((((totalSize - Days7AgoResultsStore[0]) / totalSize) * 100) < 10))
                            {
                                dublin1SevenLabel.ForeColor = Color.Black; dublin1SevenLabelDrives.ForeColor = Color.Black;
                                dublin1SevenLabel.Text = string.Format((((totalSize - Days7AgoResultsStore[0]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublin1SevenLabelDrives.Text = string.Format((totalSize - Days7AgoResultsStore[0]).ToString("#0.0") + "{1}", "+", " GB");
                            }
                            else
                            {
                                dublin1SevenLabel.ForeColor = Color.Red; dublin1SevenLabelDrives.ForeColor = Color.Red;
                                dublin1SevenLabel.Text = string.Format((((totalSize - Days7AgoResultsStore[0]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublin1SevenLabelDrives.Text = string.Format((totalSize - Days7AgoResultsStore[0]).ToString("#0.0") + "{1}", "-", " GB");
                            }
 
 
 
                            break;
 
                        case "dublmx01": //add hd space to correct listbox
                            dublmx01ListBox.Items.Add(ndivSize.ToString("#0.00"));
                            Image12.Visible = true;
                            totalSize += ndivSize;
                            //display hd size in label
                            if (((((totalSize - totalSize2[1]) / totalSize) * 100) > -5) && ((((totalSize - totalSize2[1]) / totalSize) * 100) < 5))
                            {
                                dublmx01Label.ForeColor = Color.Black; dublmx01perLabel.ForeColor = Color.Black;
                                dublmx01perLabel.Text = string.Format((((totalSize - totalSize2[1]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublmx01Label.Text = string.Format( (totalSize - totalSize2[1]).ToString("#0.0") + "{1}", "+", " GB");
                            }
 
                            else
                            {
                                dublmx01Label.ForeColor = Color.Red; dublmx01perLabel.ForeColor = Color.Red;
                                dublmx01perLabel.Text = string.Format((((totalSize - totalSize2[1]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublmx01Label.Text = string.Format((totalSize - totalSize2[1]).ToString("#0.0") + "{1}", "-", " GB");
                            }
                            //this if else checks to see that 7 days ago was drive space total above or below 5% then displays
                            // in black or red text accordingly along with the percentage of change
                            if (((((totalSize - Days7AgoResultsStore[1]) / totalSize) * 100) > -10) && ((((totalSize - Days7AgoResultsStore[1]) / totalSize) * 100) < 10))
                            {
                                dublmx01SevenLabel.ForeColor = Color.Black; dublmx01SevenLabelDrives.ForeColor = Color.Black;
                                dublmx01SevenLabel.Text = string.Format((((totalSize - Days7AgoResultsStore[1]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublmx01SevenLabelDrives.Text = string.Format( (totalSize - Days7AgoResultsStore[1]).ToString("#0.0") + "{1}", "+", " GB");
                            }
                            else
                            {
                                dublmx01SevenLabel.ForeColor = Color.Red; dublmx01SevenLabelDrives.ForeColor = Color.Red;
                                dublmx01SevenLabel.Text = string.Format((((totalSize - Days7AgoResultsStore[1]) / totalSize) * 100).ToString("#0.0") + "{0}", " %");
                                dublmx01SevenLabelDrives.Text = string.Format((totalSize - Days7AgoResultsStore[1]).ToString("#0.0") + "{1}", "-", " GB");
                            }
                            break;

Open in new window

jkinghornAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VelioCommented:
Hi

I think the DataGrid is the right way to go, using Template columns you can generate dynamic text/list boxes, which can be manipulated in the DataGrid's OnItemCreated and OnItemDatabound event handlers.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gbzhhuCommented:
This should be a simple thing to do.  What version of visual studio web express are you using?  Also you say you want to display data in textboxes but your code is dealing with ListBoxes which is confusing me.

Basically I would create a public ArrayList of Control for .NET 1.0 or 1.1 or generic list List<Control> for .NET 2.0.
Loop through datareader and add textboxes

Below is a windows form code that does what you said.  modify for webform

            List<Control> conts = new List<Control>();

            for (int i = 0; i < 10; i++)  // Loop through the datareader instead of int
            {
                tb = new TextBox();
                tb.Left = 20;
                tb.Top = prevTopLocation;
                prevTopLocation += 30;
                tb.Text = "Hello" + i.ToString();
                tb.Visible = true;
                conts.Add(tb);
                this.Controls.Add(tb);
            }
0
jkinghornAuthor Commented:
It was the way to go, thanks for the suggestion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.