Insert (Save) dynamic Panels TextBox value in database using C# and VB.Net in ASP.Net

EarlCools
 
on Oct 28, 2021 10:53 PM
Sample_114234.zip
570 Views

I have a button where the user would click to a target textbox to a panel.

I am using another button to save it to the database.

foreach (TextBox textBox in pn1TextBoxes.Controls.OfType<TextBox>())
{
    //insert textBox into database.              
}

how I have another panel creating a second textbox where the user can add comments. now I want to store both the targets and the comments in the database.

the targets are in one panel pn1TextBoxes and comments are in an another panel called pnComments. both the panel will be the same length. the problem is that I do not how to access the pnComments textbox to store them in the database.

any help please

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 28, 2021 11:50 PM

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