Dear EarlCools,
Please refer below Sample.
HTML
<asp:Panel runat="server" ID="pnlTextBoxes">
<table>
<tr>
<td>ID:</td>
<td><asp:TextBox runat="server" ID="txtID" /></td>
</tr>
<tr>
<td>Name:</td>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
</table>
</asp:Panel>
<br />
<asp:Panel runat="server" ID="pnlComments">
<table>
<tr>
<td>Feedback:</td>
<td><asp:TextBox runat="server" ID="txtFeedBack" /></td>
</tr>
<tr>
<td>Comments:</td>
<td><asp:TextBox runat="server" ID="txtComments" /></td>
</tr>
</table>
</asp:Panel>
<br />
<asp:Button runat="server" ID="btnComments" Text="Save" OnClick="OnSave" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void OnSave(object sender, EventArgs e)
{
int i = 0;
foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
{
TextBox txtComment = pnlComments.Controls.OfType<TextBox>().ToArray()[i];
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO TestTable(textBox,txtComment) VALUES (@textbox,@comment)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@textbox", textBox.Text);
cmd.Parameters.AddWithValue("@comment", txtComment.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
i++;
}
}
VB.Net
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
For Each textBox As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
Dim txtComment As TextBox = pnlComments.Controls.OfType(Of TextBox)().ToArray()(i)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO TestTable(textBox,txtComment) VALUES (@textbox,@comment)")
cmd.Connection = con
cmd.Parameters.AddWithValue("@textbox", textBox.Text)
cmd.Parameters.AddWithValue("@comment", txtComment.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
i += 1
Next
End Sub