473,471 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to deal with index out of range exception?

19 New Member
Hi,
I was trying to add two quantities at different rows of same description i.e same product_name and same item_pack.1st I want to check the table if it contains the same description or not by comparing with the first record and so on. When found, I'll add up the quantities and then to delete that particular row (to avoid duplicate search). But when I delete the row the particular row, next time it throws an out of index exception.
Below Is the given code. Any suggestions appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Try
  3.  
  4.             If con.State = ConnectionState.Closed Then
  5.                 con.Open()
  6.             End If
  7.             'Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select p.product_name, p.item_pack,p.product_manufacturer,p.product_description,w.available_qty, w.product_mrp from product_master as p inner join warehouse_master as w on p.product_name=w.product_name and p.item_pack= w.item_pack ", con)
  8.             Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select product_name , item_pack, available_qty from warehouse_master order by serial_no", con)
  9.             Dim ds As DataSet = New DataSet
  10.             Dim ds1 As DataSet = New DataSet
  11.             da.Fill(ds, "Warehouse1")
  12.             DGV1_Home.DataSource = ds.Tables(0)
  13.             DGV1_Home.Show()
  14.             Dim row, row1, rowcnt As Integer
  15.             'Dim nm, nm1, pack, pack1 As String
  16.             rowcnt = ds.Tables(0).Rows().Count - 1
  17.  
  18.             For row = 0 To rowcnt
  19.                 MessageBox.Show("inside 1st for loop" & rowcnt)
  20.                 Dim qty As Integer
  21.                 qty = ds.Tables(0).Rows(row).Item(2)
  22.                 For row1 = 1 To rowcnt
  23.                     MessageBox.Show("inside 2nd for loop" & rowcnt)
  24.                     If ds.Tables(0).Rows(row).Item(0) = ds.Tables(0).Rows(row1).Item(0) And ds.Tables(0).Rows(row).Item(1) = ds.Tables(0).Rows(row1).Item(1) Then
  25.                         MessageBox.Show("iif " & rowcnt)
  26.                         qty += ds.Tables(0).Rows(row1).Item(2)
  27.                         ds.Tables(0).Rows.RemoveAt(row1)
  28.                         rowcnt = ds.Tables(0).Rows.Count - 1
  29.  
  30.                     End If
  31.                     MessageBox.Show(rowcnt)
  32.                 Next
  33.                 MessageBox.Show(ds.Tables(0).Rows(row).Item(0) & qty)
  34.                  rowcnt = ds.Tables(0).Rows.Count - 1
  35.                 MessageBox.Show("out side 2nd loop" & rowcnt)
  36.             Next
  37.             MessageBox.Show("out side 1st for loop")
  38.             'DGV1_Home.DataSource = ds.Tables(0)
  39.             'DGV1_Home.Show()
  40.         Catch ex As Exception
  41.             MessageBox.Show(ex.ToString)
  42.         Finally
  43.             con.Close()
  44.         End Try
  45.  
  46.  
Here Is the data
Sl.No anacin stripe10 10
2 dependal stripe10 25
3 anacin stripe20 18
4 anacin stripe10 24
5 eno satchet 80
6 eno bottle 10
7 axacef stripe20 25
Feb 1 '11 #1

✓ answered by David Gluth

Hi GR,

First if you are only trying to summarize, not actually remove the duplicate rows then you can do it easier in SQL.

This query
Expand|Select|Wrap|Line Numbers
  1.  Select product_name , item_pack, SUM(available_qty) AS QTY from warehouse_master GROUP BY product_name, item_pack order by serial_no" 
will give you one row for each unique product name item pack combination and sum them for you.

If you really need to purge them for the database you need to make some changes to your loop. Right now you are saying that rowcnt = total number of rows. When you delete a row then the actual number of rows is less than it was since you set the rowcnt.
For example I have two rows. You first loop say to loop for 0 to 1. You start to process row 0. Your second loop finds a match in row 1 so you remove it. Exit to you first loop which now tries to process row 1. It doesn't know that you have deleted it so you get an error index (row(1) is out of range there is no row here anymore.

I find that unless there is a compelling reason not to I will loop backward through the outer loop
Expand|Select|Wrap|Line Numbers
  1.  for row = rowcnt to 0 step -1 
that way I start with the index that will change when I delete. Then in the inner loop I want to get a new row count as it will have changed from a previous row removal and also go backwards.
Expand|Select|Wrap|Line Numbers
  1. Rowcnt2 = ds.table(0).rows.count-1 
  2. For row1 = rowcnt2 to 0 step -1
  3.  
David

2 7247
David Gluth
46 New Member
Hi GR,

First if you are only trying to summarize, not actually remove the duplicate rows then you can do it easier in SQL.

This query
Expand|Select|Wrap|Line Numbers
  1.  Select product_name , item_pack, SUM(available_qty) AS QTY from warehouse_master GROUP BY product_name, item_pack order by serial_no" 
will give you one row for each unique product name item pack combination and sum them for you.

If you really need to purge them for the database you need to make some changes to your loop. Right now you are saying that rowcnt = total number of rows. When you delete a row then the actual number of rows is less than it was since you set the rowcnt.
For example I have two rows. You first loop say to loop for 0 to 1. You start to process row 0. Your second loop finds a match in row 1 so you remove it. Exit to you first loop which now tries to process row 1. It doesn't know that you have deleted it so you get an error index (row(1) is out of range there is no row here anymore.

I find that unless there is a compelling reason not to I will loop backward through the outer loop
Expand|Select|Wrap|Line Numbers
  1.  for row = rowcnt to 0 step -1 
that way I start with the index that will change when I delete. Then in the inner loop I want to get a new row count as it will have changed from a previous row removal and also go backwards.
Expand|Select|Wrap|Line Numbers
  1. Rowcnt2 = ds.table(0).rows.count-1 
  2. For row1 = rowcnt2 to 0 step -1
  3.  
David
Feb 2 '11 #2
GR M
19 New Member
Many a thanks David. Actually I have done it through SQL. But I wanted to show to my niece how the sql actually works through Vb.net codings. I have stuck at the for loop. I thought the condition checking is dynamic. But It was found to be static at the beginning of the loop. Thanks for the insight and also the suggestions. Also I've noticed , U have taken a great interest in my submissions. So nice of U David. Thanks Again.
Feb 7 '11 #3

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

Similar topics

16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
2
by: smith flyers | last post by:
string thingword=" testing hmmm"; string firstLetter,restOfWord; foreach (string word in thingword.Split()) { Console.WriteLine(word); // if this only use for output and the rest in...
3
by: Pengyu | last post by:
Whenever I call listView.RemoveAt(listView.Count-1), which remove the last item, I get an out of range exception. How to solve this problem? Thanks a lot, Pengyu.
0
by: Bill Johnson | last post by:
I have a further question regarding an archive post at: ...
4
by: Brad | last post by:
Now I am encountering my second very strange problem. I will describe the first one later. But my current problem lies with trying to bind to a combobox: Try If clubID <> "" And txtYear.Text <>...
0
by: Nick | last post by:
Hi people, I have a custom control combo box in a windows application that has a list of items to be displayed. there are currently 17 items that are bound to the table that acts as the data...
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
7
by: Pucca | last post by:
Hi: Below is the error I got from the 2 lines of code below. I don't understand why and how to correct it. The actionMenu.DropDownItems has 0 item in its collection at the time of the code. ...
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
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...
1
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.