@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!!!!
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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)
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