Length Cannot Be Less than Zero: Parameter Name: Length

I'm having issues with this on the live site. When i went local it worked just fine. I keep getting: Length cannot be less than zero.
Parameter name: length

Getting an error at this line: Dim firstText As String = FinalString.ToString().Substring(0, FinalString.ToString().LastIndexOf(strLast))

I'm trying to use parameters to email to a group within our company.

Public Function SendArtMail(Id As Integer, InHandsDate As DateTime, Manager As String) As Integer

        Dim objUtility As Utility = New Utility()
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(HttpContext.Current.Request.Url.AbsoluteUri), HttpWebRequest)
        req.Timeout = 6000
        Dim webResponse As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
        Dim enc As Encoding = Encoding.GetEncoding(1252)
        Dim responseStream As New StreamReader(webResponse.GetResponseStream(), enc)
        Dim reportContent As String = responseStream.ReadToEnd()

        webResponse.Close()
        responseStream.Close()

        If Not String.IsNullOrEmpty(reportContent) Then
            Dim swr = New StringWriter()
            Dim FinalString As New StringWriter()
            FinalString.Write(reportContent)
            Dim filename As String = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + String.Format("Order_{0}_Inhands{1}.doc", Id, InHandsDate.ToString("MMddyyyy"))
            If Not System.IO.File.Exists(filename) = True Then
                Dim fileStream As System.IO.FileStream
                fileStream = System.IO.File.Create(filename)
                fileStream.Close()
            End If
            Dim file As New System.IO.StreamWriter(filename)
            Dim strStart As String = "<table class=""dxflInternalEditorTable_iOS"">"
            Dim strLast As String = "<span cls=""Last"">"
            Dim firstText As String = FinalString.ToString().Substring(0, FinalString.ToString().LastIndexOf(strLast))
            Dim str As String = firstText.ToString().Substring(firstText.ToString().IndexOf(strStart))
            file.Write(str)
            file.Close()
            objUtility.SendEmail3("Sent Art Submission", "Art Submission", Manager, filename)
        End If
        Return Nothing
    End Function

Open in new window

Southern_GentlemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
>>Dim firstText As String = FinalString.ToString().Substring(0, FinalString.ToString().LastIndexOf(strLast))

it seems that FinalString.ToString().LastIndexOf(strLast) returned with an -1 ?

apparently strLast is not found in FinalString ?

you need a better handling for this, like:
Dim firstText As String
If FinalString.ToString().LastIndexOf(strLast) > -1 Then
       firstText = FinalString.ToString().Substring(0, FinalString.ToString().LastIndexOf(strLast))
End If

Open in new window

0
Southern_GentlemanAuthor Commented:
Thanks Ryan, now I am getting a:  Object reference not set to an instance of an object
For this line: Dim str As String = firstText.ToString().Substring(firstText.ToString().IndexOf(strStart))

Is this because the firstText is used before it is assigned a value.
0
Ryan ChongCommented:
can you put the define variable statements (Dim yourVariable As .... ) at the top of your Module. And then retest your scripts? like:

on top of your Module...

Dim str As String
Dim firstText As String
Dim strStart As String
...

and then:

str = firstText.ToString().Substring(firstText.ToString().IndexOf(strStart))
....

?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Southern_GentlemanAuthor Commented:
Makes sense, i just moved the End If after the file.Close() and put the dim statement in. All good. Thanks a bunch.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.