473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1261
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

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

Similar topics

7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
2
by: Alan Silver | last post by:
Hello, I have code like the following... foreach (Control ctl in Page.Controls) { if (ctl.ID.StartsWith("X_")) { // do stuff } }
7
by: steve marchant | last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec intervals, then starts from 1 again and repeats. Have two Command buttons on the form. Cmd1 starts the counting, and I need to...
1
by: =?Utf-8?B?Z2VvZmZh?= | last post by:
i am going crazy...... how can i loop through my result set and use File.Copy of File.Move to copy or move the dim'd filename????? i have tried AppendAllText, AppendText, Move, Copy, nothing works...
4
by: Tonio Tanzi | last post by:
I have the following problem in a Win 2000 Server + SQL Server 2000 environment and I hope somewhat can help me to resolve it (after many days of useless attempts I am desperate). In my database...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
1
by: TanmayQuery | last post by:
I have written a sql procedure, i want to pass delared variable @cnt in line131,char24 below is the PL Code: CREATE PROCEDURE PL @dt varchar(10), @stock as bit --1/true for updated and 0/false...
3
by: backups2007 | last post by:
Here'es my code. I double checked it and I can't seem to find any reason why it won't work. Or maybe I just missed something. Please help. Thanks. If you have any suggestion on how I can improve this...
4
by: charmeda103 | last post by:
i working on a program in c++ and my program is to read data from a file. I using a while loop. the while loop is not reading the dates of each line. mm/dd/yyyy everything in the file is reading...
2
by: thoffman | last post by:
Hello again everyone! I have an issue that I can't seem to find a straight answer for anywhere... I have a For loop that sends a file name to a background worker that contains some long running...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.