Hi All,
I am trying to insert selected contents of a listbox into a database by looping through the collection. The loop is working and the contents are going into the database. Starnge thing is i come up with this error as soon as the contents are inserted in the database!.
Dim iCount As Integer
Dim ddlcount As Integer
Dim strItem As String
For iCount = 0 To CInt(ddlSelectedLocations.Items.Count)
If ddlSelectedLocations.Items.Item(iCount).Selected <= CInt(ddlSelectedLocations.Items.Count) Then
If ddlSelectedLocations.Items.Item(iCount).Selected Then
'strItem = lst.Items(iCount).value
SqlCommand1.Parameters("@townid").Value = CInt(ddlSelectedLocations.Items(iCount).Value)
SqlCommand1.Parameters("@userid").Value = CInt(Request.QueryString("u"))
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuery()
'
SqlConnection1.Close()
End If
End If
Next
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 346:
Line 347:
Line 348: If ddlSelectedLocations.Items.Item(iCount).Selected Then
Line 349: 'strItem = lst.Items(iCount).value
Line 350:
Source File: C:\Inetpub\wwwroot\BJ\candidatereg_3.aspx.
vb Line: 348