6 Replies - 242 Views - Last Post: 12 October 2012 - 06:35 AM Rate Topic: -----

#1 fishingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 12

Weird bug in a for loop

Posted 12 October 2012 - 03:40 AM

Hello,
I am developing an application that reads data from an Access database and copying in an excel file through a datagrid to treatment. To do this I used a for loop to copy the datagrid to excel file. My problem is that the loop sometimes hangs when copying and does not complete this task and sometimes it works properly. I used two methods to the task, but I still have this problem.
The codes used are:

        Dim cont As String
            For i = 2 To lngRowCount + 1
 
                For j = 1 To 10
 
                    cont = DataGridView2.Rows(i - 1).Cells(j - 1).Value
                    If j = 1 Or j = 6 Or j = 8 Or j = 9 Or j = 10 Then
                        Excel.ActiveWorkbook.ActiveSheet.Cells(i, j).value = "'" + cont
                    Else
                        Excel.ActiveWorkbook.ActiveSheet.Cells(i, j).value = cont
 
                    End If
 
                Next
 
            Next



            Dim j As Integer
            Dim i As Integer
            i = 0
            For Each myRow2 In DataGridView2.Rows
                j = 0
                For Each myColumn2 In DataGridView2.Columns
                    Dim cont As String
                    cont = DataGridView2.Rows(i).Cells(j).Value
                    If j = 1 Or j = 6 Or j = 8 Or j = 9 Or j = 10 Then
                        Excel.ActiveWorkbook.ActiveSheet.Cells(i + 2, j + 1).value = "'" + cont
                    Else
                        Excel.ActiveWorkbook.ActiveSheet.Cells(i + 2, j + 1).value = cont
                    End If
                    j = j + 1
                Next
                i = i + 1
            Next



Thank you for helping me to understand why the loop stops although I'm sure the number of iterations that I spend for this loop.

Is This A Good Question/Topic? 0
  • +

Replies To: Weird bug in a for loop

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 224
  • View blog
  • Posts: 745
  • Joined: 19-October 11

Re: Weird bug in a for loop

Posted 12 October 2012 - 03:49 AM

What do you mean by: "the loop stops"? Does it throw exception, saying index was out of range, or something else?
Was This Post Helpful? 0
  • +
  • -

#3 fishingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 12

Re: Weird bug in a for loop

Posted 12 October 2012 - 03:59 AM

Thank you for your reply,
The application opens an excel file and starts a loop filling file. My problem is that the loop hangs during this treatment and it does not finish the task of filling without any exception or error. It just does not end the task and returns to the program, but when I check the excel file I find that there are still lines that are not copied.
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 224
  • View blog
  • Posts: 745
  • Joined: 19-October 11

Re: Weird bug in a for loop

Posted 12 October 2012 - 04:56 AM

Well, try to step through the code by hitting F8 key, and observing what is going on. I'd go with code 2, because code 1 has fixed number of iterations, and might not fit with different length tables.
Was This Post Helpful? 0
  • +
  • -

#5 fishingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 12

Re: Weird bug in a for loop

Posted 12 October 2012 - 06:14 AM

Thank you for your reply,
I've used the Try / Catch and she returned the following error message:

Exception from HRESULT : 0x800AC472
Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 224
  • View blog
  • Posts: 745
  • Joined: 19-October 11

Re: Weird bug in a for loop

Posted 12 October 2012 - 06:18 AM

Check this link.
Was This Post Helpful? 0
  • +
  • -

#7 torind_2000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 234
  • Joined: 22-August 11

Re: Weird bug in a for loop

Posted 12 October 2012 - 06:35 AM

I found this in addition to lucky3's post. Also found some people were putting their try/catches in a loop and having them try X number of times to see if it would continue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1