调用的目标抛出了异常

时间:2011-01-21 21:42:44

标签: vb.net exception substring activereports

我知道这是一个相对常见的错误,但我在一个应用程序中工作,该应用程序允许使用vb.net或C#编写自定义报告以进行脚本编写。错误处理非常差,我不知道添加自己的错误(如果可能的话)。

我的代码只检索存储在格式为 LastName,FirstName 的报表上的文本框中的值,并截断逗号后面的所有字符。此值 LastName 将放置在报表的新文本框中。这是我的代码:

Sub Detail1_Format

    Dim lastNameFirstName As String = ""
    Dim lastName As String = ""
    Dim lastNameCommaIndex As Integer=

'set lastNameFirstName value from data on report'
    lastNameFirstName = ReportUtilities.ReturnTextBoxValue(rpt,"Detail1","txtdtr_DTOOLS_CompanyName")

'find index of first comma in lastNameFirstName string'
    lastNameCommaIndex = lastNameFirstName.IndexOf(",")

'set contents of lastName using substring of lastNameFirstString'
    lastName = lastNameFirstName.SubString(0, lastNameCommaIndex)
'the error happens above when I use lastNameCommaIndex'
'to set the number of characters in my substring'

'set client name textbox value using lastName'
    ReportUtilities.SetTextBoxValue(rpt,"Detail1","txtdtr_CALC_ClientName",lastName)

End Sub

当我使用lastNameCommaIndex设置子字符串中的字符数时,会发生错误。如果我用数字替换它,报告就会正确发布。

1 个答案:

答案 0 :(得分:0)

您可能希望使用string.split,这将使这更容易

e.g。

   If Not String.IsNullOrWhiteSpace(lastNameFirstName) Then

        lastName = lastNameFirstName.Split(",".ToCharArray())(0)

    End If

如果你没有.net 4.0,那你就是编写IsNullOrWriteSpace的方法

Function IsNullOrWhiteSpace(ByVal s As String) As Boolean

    If s Is Nothing Then
        Return True
    End If

    Return s.Trim() = String.Empty

End Function