Question Converting String to Uri

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
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
VB.NET:
    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

VB.NET:
Dim url_html As New Uri("lstUrlToPdf.SelectedItem")
 
Hi,

I am not sure whether to laugh or cry with this problem? But hey, it's Christmas, so your problem is that you have your lstUrlToPdf.SelectedItem statement in quotes so it is being interpreted as a literal in your variable declaration.

It should be:-

VB.NET:
Dim url_html As New Uri(lstUrlToPdf.SelectedItem.ToString)

Notice, No Quotes!!

Hope that helps and Merry Christmas.

Cheers,

Ian
 
Hi,

I am not sure whether to laugh or cry with this problem? But hey, it's Christmas, so your problem is that you have your lstUrlToPdf.SelectedItem statement in quotes so it is being interpreted as a literal in your variable declaration.

It should be:-

VB.NET:
Dim url_html As New Uri(lstUrlToPdf.SelectedItem.ToString)

Notice, No Quotes!!

Hope that helps and Merry Christmas.

Cheers,

Ian

Hey Ian..thanks for your reply! I had tried that same query before without the .ToString() at the end, but it didn't work.

I went ahead and tried yours but that line is giving me an exception:
Object reference not set to an instance of an object.

I also tried:
Dim teststring As String = lstUrlToPdf.SelectedItem.ToString()

and received the same exception

Thanks again!
 
UPDATE::

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!
 
Back
Top