Is it possible to use multiple Repeater FOR loop insert a method?

Simflex 321 Reputation points
2023-03-10T06:41:02.06+00:00

Greetings again dear experts,

I have an issue with several moving parts.

I have a method, btnSave_Click(...) that uses a repeater FOR loop for an insert statement.

The FOR loop code is below:

For Each item As RepeaterItem In DynamicRepeater.Items

                Dim ddlAssets As DropDownList = TryCast(item.FindControl("ddlAssets"), DropDownList)
                Dim txt_Amount As TextBox = TryCast(item.FindControl("txt_Amount"), TextBox)
                Dim AssetModelNumber As TextBox = TryCast(item.FindControl("AssetModelNumber"), TextBox)
                Dim PurchaseDate As TextBox = TryCast(item.FindControl("PurchaseDate"), TextBox)
                Dim txt_newAssets As TextBox = TryCast(item.FindControl("txt_newAssets"), TextBox)

                'Prepare Assets (New and existing) for later use

                'Check the ddlAssets drodpwn to see if the Asset to be selected is on the list.If no, then insert 
                'it into the AssetsData lookup table, grab the last inserted id and use for new inserts.
                Dim tID = ddlAssets.SelectedIndex
                Dim TSize As Integer = 0
                If tID <= 0 Then 'asset to be selected is not on the list, add it.

                    Dim strQuery As String = "insert into AssetsData(Assets) VALUES(@tsizes)"
                    strQuery += " SELECT SCOPE_IDENTITY()"

                    Dim cmd1 As SqlCommand = New SqlCommand(strQuery)
                    cmd1.Connection = con
                    '  con.Open()
                    cmd1.Parameters.Add("@tsizes", SqlDbType.NVarChar).Value = txt_asset.Text)

                    'now that the new asset has been added, store the last inserted ID associated with new asset into assetid variable
                    Dim assetid As Integer = Convert.ToInt32(cmd1.ExecuteScalar())

                    'so, if a new asset was addded, use it
                    TSize = assetid
                Else 'get it from ddlAssets dropdown
                    TSize = ddlAssets.SelectedValue
                End If
                
                //A bunch of insert, delete and update statements follow
                
                
                
                
                Next

I have used this code on a single insert statement and it works flawlessly.

However, this time around, there are several insert, update and delete statements between the FOR... and NEXT.

It is a pile of mess but the code works mostly except the FOR loop.

The FOR loop does not work because only the first row is inserted into the database. Subsequent rows are ignored.

I can only blame the many insert, update and delete statements, some of which do not need the FOR loop but there is no way to separate them out.

My question is, is possible to use the same FOR loop multiple times inside a method?

For insert, is it possible to do something like:

   For Each item As RepeaterItem In DynamicRepeater.Items
   
     insert statement here
   Next

Then another insert statement that does not need the for loop, followed by an update.

Then another FOR...NEXT loop, etc?

The code is way too long to post here.

Your guidance would be all I would need if you could help me.

Thank you very much in advance.

Developer technologies .NET Other
Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2023-03-10T12:49:02.44+00:00

    Hi

    Maybe a minor and probably nothing to do with a solution to your problem. In the code you show, you are testing for an item either in/not in the list. You check for an index (If tID <= 0 Then ...... ) , so what result do you expect when first item in list is selected with index of zero? I will result in the item being added again to the list where duplicates could cause an exception elsewhere, or certainly upset a search. In such a scenario, I use the comparison < 0 which allows the index 0 to be used as a valid item in the list.

    The only thing that comes to mind. Use a Try ..... Catch around segments of your code, coupled with breakpoints to see why the For .... Next loop is failing.


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.