Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

VS 2010 Program error checking

$
0
0
Hey Guys,

What i'm trying to do is load a list of urls into a small application and do some regex on the urls, the program works as it should but there is a problem when it hits a non standard regex match "Input string was not of the correct format"

Code:

        Try

            '// Open the target file...
            OpenFileDialog1.Title = "Select accounts file..."
            OpenFileDialog1.InitialDirectory = "C:"

            '//
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

                '// Display the file in the input area...
                txtBoxInputFile.Text = OpenFileDialog1.FileName.ToString()

                '// List ready to use...
                Dim list As New List(Of String)

                '// Open file with the Using statement.
                Using sReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)

                    '// Loop over each line in file, While list is Not Nothing.
                    Do While sReader.Peek <> -1

                        '// THE READ URL IN A VARIABLE
                        Dim eachURL As String = sReader.ReadLine

                        '// MATCH THIS PATTERN
                        Dim pattern1 As String = "node/(.*)/"
                        Dim m As Match = Regex.Match(eachURL, pattern1)

                        Dim numbersReplaced As String = String.Empty

                        '// IF WE HAVE A SUCCESS/MATCH
                        If m.Success Then

                            Dim realNumber As Integer = Integer.Parse(m.Groups(1).Value) - Integer.Parse(txtRandomNumber.Text)

                            Dim val1 As String = m.Groups(1).Value
                            Dim val2 As String = realNumber.ToString

                            numbersReplaced = eachURL.Replace(val1, val2)

                            '// ADD THE RESULTS TO THE LISTBOX
                            listBoxmain.Items.Add(numbersReplaced)

                        Else

                            '// ADD THE RESULTS TO THE LISTBOX
                            listBoxmain.Items.Add(numbersReplaced)

                        End If

                    Loop

                    '// Close up the streamreader
                    sReader.Close()

                End Using

            End If

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try

All i want to do is have the program run through the list ignoring the problems urls, of a list of 1000 urls it will only add to the listbox xxx then throw the "Input string was not of the correct format" error, is there something wrong with my logic?

cheers for any help guys

Graham

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>