Solved

Excel VB.net Add in Strange Error

Posted on 2014-07-25
6
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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 will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

752 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