Converting String to Uri

fpforum

Junior Member
Joined
Apr 22, 2008
Messages
117
Reaction score
4
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")
 
.SelectedItem probably returns an object, not a string, and Uri() probably requires a string.
 
.SelectedItem probably returns an object, not a string, and Uri() probably requires a string.

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
 
SelectedItem returns an object so you should convert that to String.

Dim teststring As String = lstUrlToPdf.SelectedItem.ToString()
 
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.
 
Have you actually selected the item before you clicked the button? lol
 
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!
 
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.
 
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.
 
Back
Top