none
Keep getting error "A first chance exception of type 'System.NotImplementedException' occurred"

    General discussion

  • Hi,
     
    I implemented the below code to export data from a data grid to excel. The code compiles correctly but I get an error message "A first chance exception of type 'System.NotImplementedException' occurred" when I click on the button to export data to excel. 
     
    I am working on the trial version of Visual Studio 2010 and currently have a licensed copy of Office 2007 installed on my machine. "Microsoft.Office.Interop.Excel" reference has been correctly imported.
     
    Appreciate if someone can provide some insight into thisi ssue.
     
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            'verfying the datagridview having data or not
            If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
                Exit Sub
            End If
     
            'Creating dataset to export
            Dim dset As New DataSet
            'add table to dataset
            dset.Tables.Add()
            'add column to that table
            For i As Integer = 0 To DataGridView1.ColumnCount - 1
                dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
            Next
            'add rows to the table
            Dim dr1 As DataRow
            For i As Integer = 0 To DataGridView1.RowCount - 1
                dr1 = dset.Tables(0).NewRow
                For j As Integer = 0 To DataGridView1.Columns.Count - 1
                    dr1(j) = DataGridView1.Rows(i).Cells(j).Value
                Next
                dset.Tables(0).Rows.Add(dr1)
            Next
     
            Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
            Dim wBook As Microsoft.Office.Interop.Excel.Workbook
            Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
     
            wBook = excel.Workbooks.Add()
            wSheet = wBook.ActiveSheet()
     
            Dim dt As System.Data.DataTable = dset.Tables(0)
            Dim dc As System.Data.DataColumn
            Dim dr As System.Data.DataRow
            Dim colIndex As Integer = 0
            Dim rowIndex As Integer = 0
     
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(1, colIndex) = dc.ColumnName
            Next
     
            For Each dr In dt.Rows
                rowIndex = rowIndex + 1
                colIndex = 0
                For Each dc In dt.Columns
                    colIndex = colIndex + 1
                    excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
     
                Next
            Next
     
            wSheet.Columns.AutoFit()
            Dim strFileName As String = "C:\ss.xls"
            Dim blnFileOpen As Boolean = False
            Try
                Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
                fileTemp.Close()
            Catch ex As Exception
                blnFileOpen = False
            End Try
     
            If System.IO.File.Exists(strFileName) Then
                System.IO.File.Delete(strFileName)
            End If
     
            wBook.SaveAs(strFileName)
            excel.Workbooks.Open(strFileName)
            excel.Visible = True
        End Sub
    Friday, October 21, 2011 5:56 PM

All replies

  • If you only get a first chance exception but everything works as expected, there is no problem. Does it?
    Armin
    Friday, October 21, 2011 6:48 PM
  • But the application hangs and I have to forcibly stop it. Why isn't the export to excel functionality working as expected?
    • Edited by VikramVVV Tuesday, October 25, 2011 3:29 AM
    Tuesday, October 25, 2011 3:29 AM
  • Where does it hang? Whenever it hangs, interrupt it (press the Break button) and look what it's doing.
    Armin
    Tuesday, October 25, 2011 3:15 PM
  • When I click on the export to excel button after data has populated in the datagird, this is the piece of code where it hangs "Throw New NotImplementedException

     

    Namespace

     

    Microsoft.Office.Interop.Excel

     

    Class

    ApplicationClass

     

    Private _cells As

    String

     

    Property Cells(ByVal p1 As Integer, ByVal colIndex As Integer) As

    String

     

    Get

     

    Return

    _cells

     

    End

    Get

     

    Set(ByVal value As String

    )

    _cells = value

     

    End

    Set

     

    End

    Property

     

    Property Visible As

    Boolean

     

    Function Workbooks() As

    Object

     

    Throw New

    NotImplementedException

     

    End

    Function

     

    End

    Class

    End

     

    Namespace

     

     

     

    Wednesday, October 26, 2011 4:36 PM
  • This is hard to read. Please use the "insert code block" icon at the top of this edit box to insert code.

    Also specify the exact line where it hangs if it hangs in external code. If it's a loop inside your own, you must verify the conditions for the loop.

    Anyway, it looks like source code of an external class.... why?


    Armin
    Wednesday, October 26, 2011 5:24 PM
  • Here is the code block. The code where the app hangs is"Throw New NotImplementedException".

    I don't see an issue in the looping condition in my code and am not quite understanding why the external class should throw this error..

    Namespace Microsoft.Office.Interop.Excel
        Class ApplicationClass
    
            Private _cells As String
    
            Property Cells(ByVal p1 As Integer, ByVal colIndex As Integer) As String
                Get
                    Return _cells
                End Get
                Set(ByVal value As String)
                    _cells = value
                End Set
            End Property
    
            Property Visible As Boolean
    
            Function Workbooks() As Object
                Throw New NotImplementedException
            End Function
    
        End Class
    End Namespace
    

     


    • Edited by VikramVVV Wednesday, October 26, 2011 8:49 PM
    Wednesday, October 26, 2011 8:48 PM
  • I'm completely confused!  This looks like the source code of the Interop assembly for Microsoft Excel. This is part of your project?? Sorry, I don't get it.
    Armin
    Wednesday, October 26, 2011 8:55 PM
  • Sorry to make this co confusing. I am newbie and my project is to get data from an Oracle database into a data grid and have a button which when clicked will export this data to a excel spreadsheet and store it in C drive.

     

    I implemented this code from the link http://www.codeproject.com/KB/vb/Senthil_S__Software_Eng_.aspx.

     

    You are correct that the code that I pasted is the cource code of the Interop Assembly. The "Microsoft.Office.Interop.Excel" reference has been imported into the Project.

     

    I am not sure on how to proceed with the debugging.

     

    Wednesday, October 26, 2011 9:58 PM
  • I'm sorry, I can't handle that. I don't know where you got the source code from.

    Anyway, according to the documentation... please read the first paragraph:
    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.applicationclass.workbooks.aspx


    Armin
    Wednesday, October 26, 2011 10:08 PM
  • Thanks for your pateince. Is there any alternative  easy code  to export data from data grid to excel?
    Wednesday, October 26, 2011 10:19 PM