Search code examples
c#asp.netrepeater

Insert data from a Repeater's textbox into a database


I am using a Repeater that has a label and textbox. It generates the correct labels based on columns in a table (First Name, Last Name, Address, etc). The user is meant to type in their information into the corresponding textbox.

After this, I need a way to insert their data from the textbox into my database. However what I am unsure of is exactly how to insert each item into the correct column of the database. I am using Stored Procedures, so it would be something like:

sqlCmd.Parameters.Add("@FirstName", SqlDbType.Int).Value = firstName.Text;

Of course I cannot identify firstName.Text when the only textbox I have is a txtData repeated.

foreach (RepeaterItem rpItem in RepeaterForm.Items)
                    {
                        Label lblData = rpItem.FindControl("lblData") as Label;
                        TextBox txtData = rpItem.FindControl("txtData") as TextBox;

                        if (txtData != null)
                        {
                            sqlCmd.Connection = sqlConn;
                            sqlCmd.CommandType = CommandType.StoredProcedure;
                            sqlCmd.CommandText = "spInsFormFields";
                            sqlCmd.Parameters.Clear();
                            
                          //Code here
                           
                            sqlCmd.ExecuteNonQuery();
                        }
       }

A sample repeater form could look like this:

First Name: John

Last Name: Smith

Address: 123 Cherry Lane

And a sample of my database is like this:

Registrant table

FirstName, LastName, Address columns

Submit button should enter ONE row based on an EventId and FormId (which I collected earlier in my code)

EDIT:

Here is my Repeater code (as requested):

   <asp:Repeater ID="RepeaterForm" runat="server" OnItemCommand="RepeaterForm_ItemCommand">
   <ItemTemplate>
       <table>
            <tr>
                <td><asp:Label ID="lblData" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label></td>
                 <td><asp:TextBox ID="txtData" runat="server"></asp:TextBox>
                     <br></br></td>
       </tr>
       </table>
   </ItemTemplate>
        <FooterTemplate><asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="BtnSubmit_Click" /></FooterTemplate>
            </asp:Repeater>

Solution

  • Prepare your repeater like this:

     <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <input type="text"  name='<%#Eval("ColumnName") %>' />
                </ItemTemplate>
            </asp:Repeater>           
            <asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
        </div>
    </form>
    

    And by using this C# code you can get all the values dynamicly :

     public partial class WebForm1 : System.Web.UI.Page
    {
        List<Column> Columns = new List<Column>();
        protected void Page_Init(object sender, EventArgs e)
        {
            Columns.Add(new Column { ColumnName = "ID" });
            Columns.Add(new Column { ColumnName = "Name" });
            Columns.Add(new Column { ColumnName = "Surname" });
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.DataSource = Columns;
            Repeater1.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> Values = new Dictionary<string, string>();
            foreach (var item in Columns)
            {
                Values.Add(item.ColumnName, Request.Form[item.ColumnName]);
            }
        }
    }
    class Column
    {
        public string ColumnName { get; set; }
    }