---
title: "Get Data from a table which has been generated programmatically"  
description: "Get Data from a table which has been generated programmatically"  
author: "Anonymous User"  
published: 2014-08-26  
updated: 2014-08-26  
canonical: https://www.mindstick.com/forum/2137/get-data-from-a-table-which-has-been-generated-programmatically  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Get Data from a table which has been generated programmatically

I am creating a [Control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) Table programmatically and I want after [user clicks](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) a [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) to get the values.

The table has three columns, {Title([DropDownList](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework)), FName([TextBox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview)), LName(TextBox)} and two rows.

I [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) the values for FName, LName without [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic), but I am getting wrong value for Title, which is a DropDownList. It gives me for both Titles, the [selected value](https://www.mindstick.com/forum/525/get-selected-value-of-datagridviewcomboboxcolumn) of the second column.

More Clear,

For the 1st row --> Mr, Foo, Bar

for 2nd row- -> Mrs, Foo, Bar

after user clicks the button I am getting

For 1st row --> Mrs, Foo, Bar

For 2nd row- -> Mrs, Foo1, Bar.

This is the method of [table creation](https://www.mindstick.com/forum/156741/why-are-use-the-sql-check-constraint-in-the-database-at-table-creation)

```
private void SetPassengerDetailsTable(int roomIdentity, int? adults,        int? children, int? infants, bool setLeader = false) {        var leaderIsSet = false;        var roomTable = new Table();        roomTable.ID = "PassengerDetailsTBL_" + roomIdentity;        var trHeader = new TableHeaderRow();        var tcTitle = new TableCell();        tcTitle.Controls.Add(new Label() { Text = "<b>Title</b>" });        trHeader.Cells.Add(tcTitle);        var tcFName = new TableCell();        tcFName.Controls.Add(new Label() { Text = "<b>First Name</b>" });        trHeader.Cells.Add(tcFName);        var tcLName = new TableCell();        tcLName.Controls.Add(new Label() { Text = "<b>Last Name</b>" });        trHeader.Cells.Add(tcLName);        var tcType = new TableCell();        tcType.Controls.Add(new Label() { Text = "<bType</b>" });        trHeader.Cells.Add(tcType);        roomTable.Rows.Add(trHeader);        var listItems = new ListItem[6];        listItems[0] = new ListItem("", "") { Selected = true };        listItems[1] = new ListItem("Mr", "Mr");        listItems[2] = new ListItem("Mrs", "Mrs");        listItems[3] = new ListItem("Miss", "Miss");        listItems[4] = new ListItem("Infant", "Inf");        listItems[5] = new ListItem("Child", "Master");        if (adults != null || adults > 0) {            for (int i = 1; i <= adults; i++) {                var trBody = new TableRow();                var ddl = new DropDownList();                var leadAdultRowID = setLeader && !leaderIsSet ? "leadadult" : "adult";                ddl.ID = "room" + roomIdentity + leadAdultRowID + i + "Title";                ddl.Items.AddRange(listItems);                var tcTitleValue = new TableCell();                tcTitleValue.Controls.Add(ddl);                trBody.Cells.Add(tcTitleValue);                var tcFNameValue = new TableCell();                tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "adult" + i + "FName", Width = 170 });                trBody.Cells.Add(tcFNameValue);                var tcLNameValue = new TableCell();                tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "adult" + i + "LName", Width = 170 });                trBody.Cells.Add(tcLNameValue);                var tcTypeValue = new TableCell();                var leadAdult = setLeader && !leaderIsSet ? "Lead Adult" : "Adult";                tcTypeValue.Controls.Add(new Label() { Text = leadAdult });                trBody.Cells.Add(tcTypeValue);                roomTable.Rows.Add(trBody);                if (setLeader)                    leaderIsSet = true;            }        }        if (children != null || children > 0) {            for (int i = 1; i <= children; i++) {                var trBody = new TableRow();                var ddl = new DropDownList();                ddl.ID = "room" + roomIdentity + "child" + i + "Title";                ddl.Items.AddRange(listItems);                var tcTitleValue = new TableCell();                tcTitleValue.Controls.Add(ddl);                trBody.Cells.Add(tcTitleValue);                var tcFNameValue = new TableCell();                tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "child" + i + "FName", Width = 170 });                trBody.Cells.Add(tcFNameValue);                var tcLNameValue = new TableCell();                tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "child" + i + "LName", Width = 170 });                trBody.Cells.Add(tcLNameValue);                var tcTypeValue = new TableCell();                tcTypeValue.Controls.Add(new Label() { Text = "Child" });                trBody.Cells.Add(tcTypeValue);                roomTable.Rows.Add(trBody);            }        }        if (infants != null || infants > 0) {            for (int i = 1; i <= infants; i++) {                var trBody = new TableRow();                var ddl = new DropDownList();                ddl.ID = "room" + roomIdentity + "infan" + i + "Title";                ddl.Items.AddRange(listItems);                var tcTitleValue = new TableCell();                tcTitleValue.Controls.Add(ddl);                trBody.Cells.Add(tcTitleValue);                var tcFNameValue = new TableCell();                tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "infan" + i + "FName", Width = 170 });                trBody.Cells.Add(tcFNameValue);                var tcLNameValue = new TableCell();                tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "infan" + i + "LName", Width = 170 });                trBody.Cells.Add(tcLNameValue);                var tcTypeValue = new TableCell();                tcTypeValue.Controls.Add(new Label() { Text = "Infant" });                trBody.Cells.Add(tcTypeValue);                roomTable.Rows.Add(trBody);            }        }        PassengerDetailsPH.Controls.Add(roomTable);    }
```

Thanks

## Replies

### Reply by Sumit Kesarwani

Hi Goti, \

create a new listItems list for every instance of the DropDownList

var ddlItems = listItems.ToArray();

ddl.Items.Addrange(ddlItems);


---

Original Source: https://www.mindstick.com/forum/2137/get-data-from-a-table-which-has-been-generated-programmatically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
