About saving data into database from dynamically created controls in C# asp.net
<p>I Have created dynamic controls depending on no which i select in dropdownlist.
For that i wrote code in SelectedIndexChanged event of dropdownlist as follows:</p>

<pre><code>int N = Convert.ToInt32(ddlpassenger.SelectedValue);
Table t = new Table();
 for (int i = 0; i &lt; N; i++)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            TextBox name = new TextBox();
            name.ID = "txtname" + i;
            cell.Controls.Add(name);
            row.Cells.Add(cell);
            t.Rows.Add(row);
            PName_placeholder.Controls.Add(t);
        }
</code></pre>

<p>I have to save data inside database.
For that i have written code for getting values of that created control in button_click event as follows:</p>

<pre><code>string pname = "";
        foreach (TableRow tr in t.Rows)
        {
            foreach (TableCell tc in tr.Cells)
            {
                foreach (Control c in tc.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.ID.StartsWith("txtname"))
                        {
                            TextBox txt = (TextBox)t.FindControl(c.ID);

                            pname = txt.Text;
                            Session["pname1"] = pname;
                        }
                    }
                }
            }
        }
</code></pre>

<p>But i am not getting value here its getting as null, so what should i do?
My Edited code is as follows:</p>

<pre><code>protected void btnsave_Click(object sender, EventArgs e)
{
Table t11 = (Table)PName_placeholder.FindControl("tblname");
        string pname = "";
        foreach (TableRow tr in t11.Rows)
        {
            foreach (TableCell tc in tr.Cells)
            {
                foreach (Control c in tc.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.ID.StartsWith("txtname"))
                        {
                            TextBox txt = (TextBox)t11.FindControl(c.ID);

                            pname = txt.Text;
                            Session["pname1"] = pname;
                        }
                    }
                }
            }
        }
}

protected void ddlpassenger_SelectedIndexChanged(object sender, EventArgs e)
    {
    int N = Convert.ToInt32(ddlpassenger.SelectedValue);
    Table t = new Table();
        t.ID = "tblname";

    for (int i = 0; i &lt; N; i++)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            TextBox name = new TextBox();
            name.ID = "txtname" + i;
            cell.Controls.Add(name);
            row.Cells.Add(cell);
            t.Rows.Add(row);
            PName_placeholder.Controls.Add(t);
    }
    }
</code></pre>

<p>Asp.net c#
Thank you.</p>
