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.