Solved

Excel VB.net Add in Strange Error

Posted on 2014-07-25
6
634 Views
Last Modified: 2014-07-30
Hi

I get the following error that occurs looping through a DataGridView that has an image column and adding the data to Excel. I have no idea what this is

Transition into COM context 0x564070 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x564070 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x564298). This may cause corruption

    Public Sub Send_to_Excel(ByVal oAnchorRangeAddress As String)
        Dim oPGB As ProgressBar = Me.ProgressBar1

        Try

            Dim D As DataGridView = Me.DataGridView1

            oPGB.Minimum = 1
            oPGB.Maximum = D.RowCount

            Dim oResponse As MsgBoxResult
            If D.RowCount > 1000 Then
                oResponse = MsgBox("You are about to load " & CStr(D.RowCount) & " lines. Would you like to continue?", MsgBoxStyle.YesNo)
                If oResponse = MsgBoxResult.No Then
                    Exit Sub
                End If
            End If

            oPGB.Visible = True

            Globals.ThisAddIn.Application.Cells.RowHeight = 120
            Globals.ThisAddIn.Application.Range("1:1").ColumnWidth = 150

            Dim rowCounter As Long

            Dim c As Integer
            Dim r As Long

            'Populate column headers first
            For c = 0 To D.ColumnCount - 1
                Globals.ThisAddIn.Application.Cells(1, c + 1).value = D.Columns(c).Name
            Next

            For r = 0 To D.RowCount - 1 'Take care of rows and progess bar that relies on its count

                rowCounter = rowCounter + 1
                oPGB.Value = rowCounter
                ' Refresh the Progress Bar
                oPGB.Refresh()
                For c = 0 To D.ColumnCount - 1 'take care of columns for each row
                    Try

                    If c = 0 Then

                        If D.Rows(r).Cells(2).Value.ToString = "Yes" Then
                            Dim oID As String = D.Rows(r).Cells(1).Value
                            Call oPositionPicture(oID, r + 2, c + 1)
                        End If

                    Else
                        Globals.ThisAddIn.Application.Cells(r + 2, c).value = D.Rows(r).Cells(c).Value
                    End If
                    Catch ex As Exception
                        MsgBox("Loop Error" & ex.Message)
                    End Try
                Next
            Next

            'Globals.ThisAddIn.Application.Columns.AutoFit()


            oPGB.Visible = False

        Catch ex As Exception
            MsgBox(ex.Message & " a155")

            oPGB.Visible = False
        End Try
    End Sub

    Sub oPositionPicture(ByVal oID As String, ByVal oExcelRow As Long, ByVal oExcelCol As Integer)

        'If Me.DataGridView1.NewRowIndex < 1 Then
        'MsgBox("Please select a row in the grid")
        'Exit Sub
        'End If

        'Dim sSQL As String = "Select Photo from Photos where ID = '" & _
        'DataGridView1.CurrentRow.Cells(13).Value() & "'"

        Dim sSQL As String = "Select Photo from Photos where ID = " & oID & ""

        Dim con As New SqlConnection(My.Settings.CS)
        Dim cmd As New SqlCommand(sSQL, con)

        Try

            con.Open()

            Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))
            PictureBox1.Image = System.Drawing.Image.FromStream(ImgStream)
            Clipboard.Clear()
            Clipboard.SetData(DataFormats.Bitmap, Me.PictureBox1.Image)
            Globals.ThisAddIn.Application.Cells(oExcelRow, oExcelCol).select()
            Globals.ThisAddIn.Application.ActiveSheet.Paste()

            ImgStream.Dispose()

            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

0
Comment
Question by:murbro
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40219495
When does the error occur?  Does it always occur at the same line at the same loop?  What happens with another spreadsheet?

Just a hunch but I am wondering if this is causing it...

ImgStream.Dispose()
0
 

Author Comment

by:murbro
ID: 40222713
It happens at the same line in the loop
0
 

Author Comment

by:murbro
ID: 40222716
I took the line "ImgStream.Dispose() " and it still bombed. I use a fresh spreadsheet each time
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 10

Accepted Solution

by:
FamousMortimer earned 500 total points
ID: 40222717
Ok... which line?

Did you try commenting out imgstream.dispose?
0
 

Author Closing Comment

by:murbro
ID: 40229562
Hi, that helped. Thanks for the help
0
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40229596
Glad you got it working.

What was the problem exactly?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question