Search code examples
c#asp.netrepeateradditioninsert-update

how to insert / edit values from repeaters dynamically?


.aspx page something like this

<form id="Form1" runat="server">  
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
            <table border="0" width="600px" cellpadding="2" cellspacing="1" style="border: 1px solid maroon;">
        <tr bgcolor="maroon">
            <th>    Subject_Id    </th>
            <th>    Subject_Name    </th>
            <th>    Fill_Marks    </th>
        </tr>
</HeaderTemplate>

<ItemTemplate>
    <tr>
        <td width="100">
                <asp:TextBox ID="Subject_Id" runat="Server" Text='<%#Eval("Subject_Id")%>'></asp:TextBox>
        </td>
        <td>
                <asp:TextBox ID="Subject_Name" runat="Server" Text='<%#Eval("Subject_Name")%>'></asp:TextBox>
        </td>
        <td>
                <asp:TextBox ID="Marks" runat="server"></asp:TextBox>
        </td>
    </tr>
</ItemTemplate>

    <FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

    <asp:Button ID="btnInsert" runat="server" onclick="btnInsert_Click" Text="Insert" CommandArgument="" CommandName=""/>
    <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" Text="Update" CommandArgument="" CommandName=""/>    

    </form>

C# - Code behind...

  protected void btnInsert_Click(object sender, EventArgs e)
  {
   cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
   cn.Open();

  foreach (RepeaterItem item in Repeater1.Items)
  {
      TextBox Subject_Id = (TextBox)item.FindControl("Subject_Id");
      TextBox Subject_Name = (TextBox)item.FindControl("Subject_Name");
      TextBox Marks = (TextBox)item.FindControl("Marks");

      SqlCommand cmd = new SqlCommand("Insert into result VALUES (id='"+@Subject_Id+"',name='"+@Subject_Name+"',marks='"+@Marks+"')", cn);

      Repeater1.DataSource = cmd.ExecuteReader();
      Repeater1.DataBind();

      cn.Close();
      cmd.Connection.Close();
      cmd.Connection.Dispose();
  }
}

Now i want to insert that data into following table.....

result_table

id    varchar(10)
name  varchar(20)
marks varchar(3)

how can i perform insert and update function to retrieve data from to the database... with easy way ? ? thanks...


Solution

  • Implement the btnInsert_Click function with the following

    ** edited as per your new function

      protected void btnInsert_Click(object sender, EventArgs e)
      {
        String connectionString = ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString;
        String queryString = "";
         using (SqlConnection connection = new SqlConnection(connectionString))
        {
            foreach (RepeaterItem item in Repeater1.Items)
           {
            TextBox Subject_Id = (TextBox)item.FindControl("Subject_Id");
            TextBox Subject_Name = (TextBox)item.FindControl("Subject_Name");
            TextBox Marks = (TextBox)item.FindControl("Marks");
    
            queryString = "Insert into result VALUES (id='"+@Subject_Id+"',name='"+@Subject_Name+"',marks='"+@Marks+"')";
    
            SqlCommand command = new SqlCommand(queryString, connection);
    
            // execute the query to update the database
            cmd.ExecuteNonQuery();
            }
        }
        //call the function to load the gridview if it is lost on postback.
      }
    

    It is always better to move the database and grid view filling codes to separate functions, so that it is easy to repeat the actions on button clicks.