Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2007, 07:26 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop problem! Please help me!!

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


 
Old March 19th, 2007, 07:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

the error is clear... you are looping for one item more than the collection count...

since it's a base 0 collection, you go from 0 to count -1 because if you have only one element the index of it is 0, if you have 2 elements the index are 0 and 1, etc....

so.. the only thing you have to change is:

Code:
For iCount = 0 To CInt(ddlSelectedLocations.Items.Count)-1
HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
 
Old March 19th, 2007, 07:38 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 God bless you gbianchi , you were right.... it worked. Thanks a million!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Have a problem in For-each loop LeoMathew XSLT 2 July 16th, 2008 05:20 AM
loop problem smilesmita Pro PHP 1 November 2nd, 2007 02:15 AM
problem of infinite loop sarah lee ASP.NET 1.0 and 1.1 Basics 19 December 14th, 2006 09:26 AM
while loop problem shoakat Classic ASP Databases 1 September 21st, 2004 03:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.