Retrieving data from a string giving error

lkrterp

Member
Joined
Jul 10, 2007
Messages
21
Programming Experience
Beginner
Hi All!

Trying to run the following code but am receiving an string to integer error. Basically, I am running a report through code and trying to retrieve a number from a line of code by removing the line description and spaces. Having problems trying to figure out how to do that.

VB.NET:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strURL As String
        Dim byteResponse As Byte()
        Dim strResponse As String
        Dim finalURL As String
        Dim CustomerClass As String = "DMO"
        Dim BeginDate As DateTime = "6/1/2009"
        Dim EndDate As DateTime = "6/30/2009"
        Dim rsDB As String = "Test"
        Dim rsUN As String = "userid"
        Dim rsPW As String = "afjlafja;jf"


        strURL = "http://ether/cgi-bin/reportsrv.cgi?DATABASE=" & rsDB
        strURL += "&PASSWORD=" & SafeStr(rsPW)
        strURL += "&USERNAME=" & SafeStr(rsUN)
        strURL += "&REPORTEXE=arrrun02&CATEGORY=ARR&ACTION=GO&REPORT=Price Protection Detail|arrprpro|armenu|&REPORTNAME=arrprpro&ENTRYORDER=PARMf1|PARMf2|PARMf3|&COLCOUNT=3&ENTRYTYPE=STANDARD|DATE|DATE|"
        finalURL = strURL
        finalURL += "&PARMf1=" & SafeStr(CustomerClass)
        finalURL += "&PARMf2=" & BeginDate.ToShortDateString
        finalURL += "&PARMf3=" & EndDate.ToShortDateString
       

        Dim wc As New System.Net.WebClient

        byteResponse = wc.DownloadData(finalURL)
        strResponse = System.Text.Encoding.ASCII.GetString(byteResponse)
        Dim sArryResponse As String() = strResponse.Split(vbLf)
        strResponse = ""
        Dim TicketBegun As Boolean = False
        Try
            For Each line As String In sArryResponse

                If line.StartsWith("ARRPRPRO") Then
                    TicketBegun = True
                End If
                
                If line.Contains("Grand Total") Then
                    strResponse += vbLf & line
                End If

            Next

            If strResponse = "" Then
                strResponse = 0
            Else
                [U]strResponse = strResponse.Remove("Grand Total").Trim[/U]            
End If
        Catch ex As Exception
            strResponse = ex.Source & "  " & ex.Message
        End Try

        Label1.Text = strResponse

    End Sub

    Private Function SafeStr(ByVal iStr As String) As String
        Return iStr.Replace("'", "\'")
    End Function


Any help would be greatly appreciated. The line of code returned is: " Grand Total 2,456.25 " and I want to retrieve just the number. The underlined code is where I am receiving my error.

Thanks
 
Use Option Strict On - it would not let you compile this code.

Look at the overload options on Remove - none of them include a string. Remove will remove a certain number of characters, either from the start or starting from the location you specify.

What you are probably looking for is Replace (OldValue as string, NewValue as string) - ie in your case:-

VB.NET:
strResponse = strResponse.Replace("Grand Total", "")
 
Thanks Inertiam!

Stupidity on my part. I should have been using Replace instead of Remove. I also tried

strResponse = Regex.Replace(strResponse, "[^0-9]", "")

but it dropped my decimal point in the amount. Does anyone know how to use the Regex.Replace and keep the decimal point in a number that is an amount?
 
Replace the comma with an empty string.

Regex not needed:


Dim total As String = "2,456.25"
total = total.Replace(",", "")
MessageBox.Show(total)


This will also work with any number of commas, such as "2,456,789.25"
 
Back
Top