Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VB.net Add in Strange Error

Posted on 2014-07-25
6
Medium Priority
?
691 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:Murray Brown
[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:Murray Brown
ID: 40222713
It happens at the same line in the loop
0
 

Author Comment

by:Murray Brown
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 2000 total points
ID: 40222717
Ok... which line?

Did you try commenting out imgstream.dispose?
0
 

Author Closing Comment

by:Murray Brown
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

664 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