How to deal with index out of range exception?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GR M
    New Member
    • Dec 2010
    • 19

    How to deal with index out of range exception?

    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.

    Code:
    Try
    
                If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
                '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)
                Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select product_name , item_pack, available_qty from warehouse_master order by serial_no", con)
                Dim ds As DataSet = New DataSet
                Dim ds1 As DataSet = New DataSet
                da.Fill(ds, "Warehouse1")
                DGV1_Home.DataSource = ds.Tables(0)
                DGV1_Home.Show()
                Dim row, row1, rowcnt As Integer
                'Dim nm, nm1, pack, pack1 As String
                rowcnt = ds.Tables(0).Rows().Count - 1
    
                For row = 0 To rowcnt
                    MessageBox.Show("inside 1st for loop" & rowcnt)
                    Dim qty As Integer
                    qty = ds.Tables(0).Rows(row).Item(2)
                    For row1 = 1 To rowcnt
                        MessageBox.Show("inside 2nd for loop" & rowcnt)
                        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
                            MessageBox.Show("iif " & rowcnt)
                            qty += ds.Tables(0).Rows(row1).Item(2)
                            ds.Tables(0).Rows.RemoveAt(row1)
                            rowcnt = ds.Tables(0).Rows.Count - 1
    
                        End If
                        MessageBox.Show(rowcnt)
                    Next
                    MessageBox.Show(ds.Tables(0).Rows(row).Item(0) & qty)
                     rowcnt = ds.Tables(0).Rows.Count - 1
                    MessageBox.Show("out side 2nd loop" & rowcnt)
                Next
                MessageBox.Show("out side 1st for loop")
                'DGV1_Home.DataSource = ds.Tables(0)
                'DGV1_Home.Show()
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            Finally
                con.Close()
            End Try
    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
  • David Gluth
    New Member
    • Oct 2010
    • 46

    #2
    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
    Code:
     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
    Code:
     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.
    Code:
    Rowcnt2 = ds.table(0).rows.count-1 
    For row1 = rowcnt2 to 0 step -1
    David

    Comment

    • GR M
      New Member
      • Dec 2010
      • 19

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

      Comment

      Working...