Only One Row is getting inserted into the database instead of multiple rows in Repeater control. Any ideas?

Simflex 301 Reputation points
2023-01-05T15:19:04.283+00:00

Greetings again,

I hope you can assist me one more time.

I am using Repeater control to dynamically add rows to the table using DropDownList.

The DropDownList has only 3 values.

A user selects value="1", one row is created. A user selects value="2", two rows are created; a user selects value="3", three rows are created.

Whether the user adds three rows or less depends on his/her needs and depends on what number is selected from the DropDownList.

This part works great, thanks in large part to @AgaveJoe 's assistance with how to dynamically add rows using Repeater from the link below:

https://learn.microsoft.com/en-us/answers/questions/1150293/how-do-i-modify-this-jquery-to-allow-for-more-cont.html?childToView=1150618#answer-1150618

I have tried to insert rows generated into the database but only the first row is getting inserted.

For instance, a user can dynamically add up to 3 rows by selecting the number of rows to add from a dropdownlist, fill out the boxes and submit to the database but so far, only the first row is getting inserted into the database.

Any ideas what I need to change with the code below?

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)  
    Dim con As SqlConnection = New SqlConnection(constring)  
    con.Open()  
    For Each item As RepeaterItem In DynamicRepeater.Items  
        Dim ddlWater As DropDownList = TryCast(item.FindControl("ddlWater"), DropDownList)  
        Dim txt_amount As TextBox = TryCast(item.FindControl("txt_amount"), TextBox)  
        Dim Item1 As TextBox = TryCast(item.FindControl("Item1"), TextBox)  
        Dim Item2 As TextBox = TryCast(item.FindControl("Item2"), TextBox)  
        If Not String.IsNullOrEmpty(ID) Then  
            Dim strQuery As String  
            strQuery = "Insert INTO WaterResource (WaterID, Item1, Item2) Values (@ddlwater, @item2, @item2)"  
            cmd = New SqlCommand(strQuery, con)  
            cmd.Parameters.AddWithValue("@ddlwater", ddlWater.SelectedValue)  
            cmd.Parameters.AddWithValue("@item1", Item1.Text)  
            cmd.Parameters.AddWithValue("@item2", Item2.Text)  
        End If  
    Next  
    cmd.ExecuteNonQuery()  
    con.Close()  
End Sub  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,566 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,205 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,767 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Simflex 301 Reputation points
    2023-01-05T15:50:21.683+00:00

    @AgaveJoe , you are absolutely right about why only one record is being inserted.

    I am just too dumb!

    Thank you - again.

    It is working now.

    Thank you!!!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.