470,627 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,627 developers. It's quick & easy.

vb.net insert and loop problem Please help!!

3
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!.

Expand|Select|Wrap|Line Numbers
  1.      Dim iCount As Integer
  2.         Dim ddlcount As Integer
  3.         Dim strItem As String
  4.  
  5.         For iCount = 0 To CInt(ddlSelectedLocations.Items.Count)
  6.  
  7.             If ddlSelectedLocations.Items.Item(iCount).Selected <= CInt(ddlSelectedLocations.Items.Count) Then
  8.  
  9.  
  10.                 If ddlSelectedLocations.Items.Item(iCount).Selected Then
  11.                     'strItem = lst.Items(iCount).value
  12.  
  13.                     SqlCommand1.Parameters("@townid").Value = CInt(ddlSelectedLocations.Items(iCount).Value)
  14.                     SqlCommand1.Parameters("@userid").Value = CInt(Request.QueryString("u"))
  15.                     SqlConnection1.Open()
  16.                     SqlCommand1.ExecuteNonQuery()
  17.                     '
  18.                     SqlConnection1.Close()
  19.  
  20.                 End If
  21.             End If
  22.  
  23.         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
Mar 19 '07 #1
2 1067
kenobewan
4,871 Expert 4TB
Welcome to the site. Have you tried ddlSelectedLocations.Items.Count - 1?
Mar 19 '07 #2
ansumansahu
149 100+
Welcome to the site. Have you tried ddlSelectedLocations.Items.Count - 1?

Yes this should solve the issue ddlSelectedLocations.Items.Count -1
Mar 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Alan Bashy | last post: by
7 posts views Thread by steve marchant | last post: by
1 post views Thread by =?Utf-8?B?Z2VvZmZh?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.