In this article I will explain with an example, how to save (insert) Dynamic TextBox Value (Text) to SQL Server database table in ASP.Net using C# and VB.Net.
 
 
Database
I have made use of the following table Names with the schema as follows.
Save (Insert) Dynamic TextBox Value (Text) to database in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of:
Panel – For adding Dynamic TextBoxes.
Button – One for adding TextBoxes and other one for saving the values of TextBoxes.
<asp:Panel ID="pnlTextBoxes" runat="server"></asp:Panel>
<hr />
<asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />
 
 
Dynamically creating and adding TextBoxes on Button Click
AddTextBox
When the Add Button is clicked, first the count of all the TextBoxes present in the Panel control is determined and then the Count is incremented to generate an Index.
The Index will be used for generating unique ID for the TextBox control.
Note: It is very important to give a common prefix (example txtDynamic) for all TextBoxes as it will be used to find and recreate all Dynamic TextBoxes on PostBack.
 
CreateTextBox
The CreateTextBox method accepts unique ID as a parameter.
Inside this method, an object of TextBox class is created and unique ID is assigned to the ID property of the TextBox.
Finally, the TextBox is appends to the Panel control using Add method.
C#
protected void AddTextBox(object sender, EventArgs e)
{
    int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
    this.CreateTextBox("txtDynamic" + index);
}
 
private void CreateTextBox(string id)
{
    TextBox txt = new TextBox();
    txt.ID = id;
    pnlTextBoxes.Controls.Add(txt);
 
    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlTextBoxes.Controls.Add(lt);
}
 
VB.Net
Protected Sub AddTextBox(sender As Object, e As EventArgs)
    Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)().ToList().Count + 1
    Me.CreateTextBox("txtDynamic" & index)
End Sub
 
Private Sub CreateTextBox(id As String)
    Dim txt As New TextBox()
    txt.ID = id
    pnlTextBoxes.Controls.Add(txt)
 
    Dim lt As New Literal()
    lt.Text = "<br />"
    pnlTextBoxes.Controls.Add(lt)
End Sub
 
 
Retaining the Dynamic TextBoxes on PostBack
In order to retain the Dynamic TextBoxes across PostBacks, the PreInit event of the Page is used to recreate the Dynamic TextBoxes using the Request.Form collection.
Initially, the keys containing the Dynamic TextBoxes are fetched using Lambda expression.
Then, using a FOR EACH loop, one by one, each Dynamic TextBox is recreated.
C#
protected void Page_PreInit(object sender, EventArgs e)
{
    List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
    int i = 1;
    foreach (string key in keys)
    {
        this.CreateTextBox("txtDynamic" + i);
        i++;
    }
}
 
VB.Net
Protected Sub Page_PreInit(sender As Object, e As EventArgsHandles Me.PreInit
    Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
    Dim i As Integer = 1
    For Each key As String In keys
        Me.CreateTextBox("txtDynamic" & i)
        i += 1
    Next
End Sub
 
 
Saving values of dynamically created TextBoxes to database in ASP.Net
When the Save Button is clicked, a FOR EACH loop is executed over all the TextBoxes present inside the Panel control.
Inside the loop, the value of each TextBox is fetched and inserted into SQL Server database table using ADO.Net.
Note: For more details on how to insert records to database in ADO.Net, please refer my article Insert data into Database in ASP.Net using C# and VB.Net.
 
C#
protected void Save(object sender, EventArgs e)
{
    foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Names(Name) VALUES(@Name)"))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Name", textBox.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}
 
VB.Net
Protected Sub Save(sender As Object, e As EventArgs)
    For Each textBox As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("INSERT INTO Names(Name) VALUES(@Name)")
                cmd.Connection = con
                cmd.Parameters.AddWithValue("@Name"textBox.Text)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    Next
End Sub
 
 
Screenshots
Dynamic TextBoxes
Save (Insert) Dynamic TextBox Value (Text) to database in ASP.Net using C# and VB.Net
 
Values of Dynamic TextBoxes saved in database Table
Save (Insert) Dynamic TextBox Value (Text) to database in ASP.Net using C# and VB.Net
 
 
Downloads