开发者

Exception has been thrown by the target of an invocation

I know this is a relatively common error but I am开发者_StackOverflow社区 working in an application that allows custom reports to be written using vb.net or C# for scripting. Error handling is very poor and I am not knowledgable enough to add my own (if it is even possible).

My code simply retrieves a value that is stored in a textbox on the report formatted as LastName, FirstName and truncates all characters after the comma. This value LastName is placed in a new textbox on the report. Here is my code:

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

The error happens when I use lastNameCommaIndex to set the number of characters in my substring. If I replace it with a number the report is published properly.


You might want to use string.split it will make this a lot easier

e.g.

   If Not String.IsNullOrWhiteSpace(lastNameFirstName) Then

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

    End If

Here's how you write IsNullOrWriteSpace if you don't have .net 4.0

Function IsNullOrWhiteSpace(ByVal s As String) As Boolean

    If s Is Nothing Then
        Return True
    End If

    Return s.Trim() = String.Empty

End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜