vb.net mscorlib.dll 中发生了“System.InvalidCastException”类型的第一次机会异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18341691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:44:24  来源:igfitidea点击:

A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll

vb.netexcelexport-to-excel

提问by Cyberwolf

I am getting an error message when running the below code it occurs on the rng = .Range.Copy From Recordset line. The error is "A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll". I know it isn't the output as the code that is commented out works fine albeit very slow. I was working on a way to speed this up. Here is the code

运行以下代码时,我收到一条错误消息,它发生在 rng = .Range.Copy From Recordset 行上。错误是“在 mscorlib.dll 中发生了类型为‘System.InvalidCastException’的第一次机会异常”。我知道这不是输出,因为被注释掉的代码虽然很慢但工作正常。我正在研究一种加快速度的方法。这是代码

    Private Sub ButRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButRun.Click
    Dim DWPROD As String = "Data Source=DWPROD;User ID=gayloj01;password=jgay0421;"
    Dim Conn As New Oracle.DataAccess.Client.OracleConnection
    Dim cmd As New Oracle.DataAccess.Client.OracleCommand
    Dim ds As New DataSet
    Dim dtAdapter As New Oracle.DataAccess.Client.OracleDataAdapter
    Dim Excel As Microsoft.Office.Interop.Excel.Application
    Dim rng As Microsoft.Office.Interop.Excel.Range

    SQLBuilder()
    Conn.ConnectionString = DWPROD
    With cmd
        .Connection = Conn
        .CommandText = strSQL
        .CommandType = CommandType.Text
    End With
    dtAdapter.SelectCommand = cmd
    ds.Tables.Add("RawData")
    dtAdapter.Fill(ds, "RawData")
    Try
        Excel = CType(GetObject(, "Excel.Application"), Microsoft.Office.Interop.Excel.Application)
    Catch ex As Exception
        Excel = New Microsoft.Office.Interop.Excel.Application
    End Try
    Try
        With Excel
            .SheetsInNewWorkbook = 1
            .Workbooks.Add()
            .Worksheets(1).Select()
            .Visible = True
            .ScreenUpdating = False
            rng = .Range(.Cells(1, 1), .Cells(ds.Tables("RawData").Rows.Count, ds.Tables("RawData").Columns.Count))
            rng.CopyFromRecordset(ds.Tables("RawData"), ds.Tables("RawData").Rows.Count, ds.Tables("RawData").Columns.Count)
            'Dim i As Integer = 1
            'For col = 0 To ds.Tables("RawData").Columns.Count - 1
            '.Cells(1, i).value = ds.Tables("RawData").Columns(col).ColumnName
            '.Cells(1, i).EntireRow.Font.Bold = True
            'i += 1
            'Next
            'i = 2
            'Dim j As Integer = 1
            'For col = 0 To ds.Tables("RawData").Columns.Count - 1
            'i = 2
            'For row = 0 To ds.Tables("RawData").Rows.Count - 1
            '.Cells(i, j).Value = ds.Tables("RawData").Rows(row).ItemArray(col)
            'i += 1
            'Next
            'j += 1
            'Next
            .ScreenUpdating = True
        End With
    Catch ex As Exception
        Debug.Print(ex.Message)
    End Try

End Sub

I am thinking it has to do with how I am filling the range, but I can't fiund any issues with it. I am very new to VB.net and would appreciate any help that can be provided.

我认为这与我如何填充范围有关,但我找不到任何问题。我对 VB.net 很陌生,如果能提供任何帮助,我将不胜感激。

TIA

TIA

Jim G

吉姆

回答by Karl Anderson

First-chance exceptions happen when you are debugging and it is the .NET Framework's way of giving the debugger (i.e. Visual Studio) an opportunity to handle the problem. If the debugger is unable to resolve the first-chance exception, then it will become an exception that your code is expected to handle. If your code does not handle the exception, then ultimately the .NET Framework will handle it (i.e. yellow screen of death in ASP.NET).

调试时会发生第一次机会异常,这是 .NET Framework 为调试器(即 Visual Studio)提供处理问题机会的方式。如果调试器无法解决第一次机会异常,那么它将成为您的代码应该处理的异常。如果您的代码不处理异常,那么最终 .NET Framework 会处理它(即 ASP.NET 中的黄屏死机)。

You can ignore first-chance exceptions, because they are intended for the debugger. In other words, your exception code (try-catchlogic) will not let you interact with first-chance exceptions.

您可以忽略第一次机会异常,因为它们是供调试器使用的。换句话说,您的异常代码(try-catch逻辑)不会让您与第一次机会异常进行交互。