1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Converting String to Uri

Discussion in 'Visual Basic .NET' started by fpforum, Dec 25, 2012.

  1. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Hey everyone..Hopefully someone can shed some light into this for me.


    I'm working on a small program that will take a URL and convert it into a PDF. I already have the url/pdf converting component referenced and it works fine. However, when I try to set it so it pulls the URLs from a listbox I keep getting the exception 'Invalid URI: The format of the URI could not be determined.' The format of the url is proper with http://...

    Here is some of my code
    Code:
        Private Sub btnConvertUrlToPdf_Click(sender As System.Object, e As System.EventArgs) Handles btnConvertUrlToPdf.Click
            Dim url_html As New Uri("lstUrlToPdf.SelectedItem")
            ProgressBar6.Maximum = lstUrlToPdf.Items.Count
            ProgressBar6.Step = 1
    
            If lstUrlToPdf.Items.Count = 0 Then
                MsgBox("Please Add At Least One URL File To Convert!")
            ElseIf txtUrlToPdf.Text = "" Then
                MsgBox("Please Select Your Destination Folder!")
            Else
                'call sub to perform file conversion
                For x As Integer = 0 To lstUrlToPdf.Items.Count - 1
                    lstUrlToPdf.SetSelected(x, True)
                    OpenURL(url_html)
                    ProgressBar6.PerformStep()
                Next x
                MsgBox("Conversion Completed Successfully!")
                ProgressBar6.Value = 0
            End If
        End Sub
    
    This is the particular line giving me the exception

    Code:
    Dim url_html As New Uri("lstUrlToPdf.SelectedItem")
    
     
  2. WCO12

    WCO12 Junior Member

    Joined:
    Feb 1, 2012
    Messages:
    170
    Likes Received:
    91
    Occupation:
    Empire building
    Location:
    Columbus, OH, USA
    .SelectedItem probably returns an object, not a string, and Uri() probably requires a string.
     
  3. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Thanks for your reply...Setting it as a string gives a new exception:
    Value cannot be null.
    Parameter name: uriString

    my code now
    Code:
        Private Sub btnConvertUrlToPdf_Click(sender As System.Object, e As System.EventArgs) Handles btnConvertUrlToPdf.Click
            'Dim url_html As New Uri("lstUrlToPdf.SelectedItem")
            Dim teststring As String = lstUrlToPdf.SelectedItem
            Dim url_html As New Uri(teststring)
            ProgressBar6.Maximum = lstUrlToPdf.Items.Count
            ProgressBar6.Step = 1
    
            If lstUrlToPdf.Items.Count = 0 Then
                MsgBox("Please Add At Least One URL File To Convert!")
            ElseIf txtUrlToPdf.Text = "" Then
                MsgBox("Please Select Your Destination Folder!")
            Else
                'call sub to perform file conversion
                For x As Integer = 0 To lstUrlToPdf.Items.Count - 1
                    lstUrlToPdf.SetSelected(x, True)
                    OpenURL(url_html)
                    ProgressBar6.PerformStep()
                Next x
                MsgBox("Conversion Completed Successfully!")
                ProgressBar6.Value = 0
            End If
        End Sub
    
     
  4. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    SelectedItem returns an object so you should convert that to String.

    Dim teststring As String = lstUrlToPdf.SelectedItem.ToString()
     
  5. M2Money

    M2Money Newbie

    Joined:
    Nov 28, 2008
    Messages:
    34
    Likes Received:
    6
    Remove the quotes ("")
    Code:
    Dim url_html As New Uri(lstUrlToPdf.SelectedItem)
     
  6. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Thanks for both your replies. I've tried both suggestions and they give the same exception:
    Object reference not set to an instance of an object.

    I've even tried
    Dim url_html As New Uri(lstUrlToPdf.SelectedItem.ToString)

    and received the same exception.
     
  7. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    Have you actually selected the item before you clicked the button? lol
     
  8. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    This ^^^^
     
  9. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    I put some code in the add url button so it selects the url as soon as it's added into the listbox..now when the convert button is clicked the first item in the listbox is already selected. That seamed to fix the issue..

    thanks again!
     
  10. ampedsoftware

    ampedsoftware Newbie

    Joined:
    Dec 25, 2012
    Messages:
    26
    Likes Received:
    12
    Your error is due to structuring your code incorrectly, it could be solved by moving the
    Code:
    Dim url_html As New Uri(lstUrlToPdf.SelectedItem)
    to just before the
    Code:
    OpenURL(url_html)
    as the code modification you describe will not fix the problem that you need the url_html variable to be re-set in every iteration of that loop anyway. Yes your code will force a selection so it won't crash, but if you select multiple items it will just use the first URL multiple times.
     
    • Thanks Thanks x 2
  11. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    The above poster is right, you need to assign the value of url everytime inside the loop instead of assigning it once above the loop. Even though you change the selected item in the loop, the new value of url is not updated unless you change the code as suggested above.
     
    • Thanks Thanks x 1