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

Simflex 111 Reputation points

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:

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)  
    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  
End Sub  
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,851 questions
ASP.NET Web Forms
ASP.NET Web Forms
A part of the ASP.NET web application framework that can be used to create ASP.NET web applications.
414 questions
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.
7,044 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Simflex 111 Reputation points

    @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!!!!

    No comments