Weird bug in a for loop

fishingman

New member
Joined
Oct 12, 2012
Messages
4
Programming Experience
1-3
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:

VB.NET:
        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

VB.NET:
            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.
 
Hi,

On the basis that you do not describe the specific error when the "loop sometimes hangs" I am guessing that you do not know what the error is?

Firstly, try encasing your code in a Try/Catch statement to try and identify what your problem is. i.e.

VB.NET:
Try
    Your code Here..... 
Catch ex As Exception
    MsgBox(ex.Message)
End Try
If you are still struggling then please post a chunk of the data in the DataGridView and any error message displayed from the above and we will see if we can find anything out of the ordinary.

Good Luck and cheers,

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

Exception from HRESULT : 0x800AC472
 
My application uses Visual Studio 2010 and I communicate in this function in Microsoft Excel 2007 using Microsoft.Office.Interop.Excel reference
 
OK, I am guessing then that this is some sort of error returned from excel rather than VS?

A bit thuck here then, is there any common point of failure that you can put your finger on? If so, can you paste some data with that common point of failure otherwise please post a nice batch of data that we can play with.

Cheers,

Ian
 
I attached my response excel file that contains the copied data and the full code of the function that I use. You can see that the excel file is not complete and the copy was interrupted.
 

Attachments

  • exemple_code.txt
    5.9 KB · Views: 20
  • exemple.zip
    135.3 KB · Views: 21
Back
Top