473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vb.net insert and loop problem Please help!!

3 New Member
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.Argument OutOfRangeExcep tion: 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 ddlSelectedLoca tions.Items.Ite m(iCount).Selec ted Then
Line 349: 'strItem = lst.Items(iCoun t).value
Line 350:


Source File: C:\Inetpub\wwwr oot\BJ\candidat ereg_3.aspx.vb Line: 348
Mar 19 '07 #1
2 1294
kenobewan
4,871 Recognized Expert Specialist
Welcome to the site. Have you tried ddlSelectedLoca tions.Items.Cou nt - 1?
Mar 19 '07 #2
ansumansahu
149 New Member
Welcome to the site. Have you tried ddlSelectedLoca tions.Items.Cou nt - 1?

Yes this should solve the issue ddlSelectedLoca tions.Items.Cou nt -1
Mar 19 '07 #3

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

Similar topics

7
2392
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 me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
2
1623
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
7121
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 know how to stop it with Cmd2. Here's my code so far: Private Sub Command1_Click() Dim x, y, m m = 1 Do Print m
1
1669
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 except the first file. I understand that the destination must be named, but why can't the destination name = my variable??? i know it works cuz i used the MsgBox's to verify.......
4
14140
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 I have two table: - master(id, field1, field2, ...) - detail(id0, id, progr, data, sede, esecutori, brani_autori) in a master-detail relation with "id" as foreign key. The fields of the "detail" table are:
6
3326
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, EmpName,DeptID,DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName, NoOfEmployees)...
1
1174
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 for previous executed data AS set dateformat dmy set nocount on
3
1652
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 code, please post your them. //inserting so if($cust_id!="") { $query_dealer = mysql_query("INSERT INTO service_order (so_no, cust_id, prod_id, qty, purchase_date, invoice_no, date_arrived, time_arrived, date_started, time_started,...
4
1231
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 accept the begining of each line which starts with the date. How do i fix it to make the program read the whole file. #include <iostream> #include <iomanip>
2
5799
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 code and I can't find a way for the parent thread to wait for the background worker to finish... Here is the example code: For i = 0 to 4 BacgroundWorker1.RunWorkerAsync(fileName(i)) Next Private Sub BackgroundWorker1(ByVal sender As...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.