locked
Value of type string cannot be converted to system.uri? RRS feed

  • Question

  • I am building a simple web browser in VB 2013, in Windows 8.1. I used a video on youtube to guide me through the process of creating this browser. 

    When my coding was done, I pressed Local Machine and got a build error. The error is located in the area where the code wants the WebView to Navigate to a URL via TextBox.text. The error message I got: The value of type string cannot be converted to system.uri. 

    Here is my code:

    Private Sub TextBox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TextBox.TextChanged
            WebView.Navigate(TextBox.Text)

    Please, use easy to understand language lol.

    Tuesday, December 31, 2013 11:07 PM

Answers

  • Hi,

     I just wanted to add that you can`t do this in the TextBox1.TextChanged event. Every time you type a character in the textbox or remove a character from it then the TextBox1.TextChanged event is triggered and it tries to navigate to the webpage and if you don`t have the URL all typed in then it will give you an error.

    For example say i want to go to "www.Google.com". As soon as i type the first "w" it will trigger the TextChanged event and when the WebBrowser tries navigating to "w" it will throw an error.

    Also you should name your textbox to something that will identify it like "TextBox_URL" or something so that you can easily tell which textbox is for what.


    • Edited by IronRazerz Wednesday, January 1, 2014 1:20 AM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:42 AM
    Wednesday, January 1, 2014 1:18 AM
  • Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim uriString As String = "ylabnez.xsb/retddg"
            'Dim uriString As String = "http://www.google.com"
    
            If Uri.IsWellFormedUriString(uriString, UriKind.Absolute) Then
                Dim MyURI As Uri = New Uri(uriString)
                MessageBox.Show("Success converting " & uriString & " to a valid URI", "All is well", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Cannot convert " & uriString & " to a valid URI", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub
    End Class
    try as is and then comment out the nonsense on and uncomment the google one
    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:40 AM
    Tuesday, December 31, 2013 11:55 PM
  • The error message I got: The value of type string cannot be converted to system.uri. 

    What type of object is WebView?  If it's a WebBrowser object then this is the method you are using. 
    http://msdn.microsoft.com/en-us/library/40x214wa(v=vs.110).aspx

    A string should be a valid type for the argument for this method - if you are getting an error then perhaps the string that is being passed is not a valid uri.  You can do the conversion explicitly using
            WebView.Navigate(New Uri(TextBox1.Text))

    To test if the string can be converted to a valid uri then see here:
    http://msdn.microsoft.com/en-us/library/ms131572(v=vs.110).aspx
    http://msdn.microsoft.com/en-us/library/system.uri.trycreate(v=vs.110).aspx

    Th

    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:40 AM
    Wednesday, January 1, 2014 12:00 AM
  • I am building a simple web browser in VB 2013, in Windows 8.1. I used a video on youtube to guide me through the process of creating this browser. 

    When my coding was done, I pressed Local Machine and got a build error. The error is located in the area where the code wants the WebView to Navigate to a URL via TextBox.text. The error message I got: The value of type string cannot be converted to system.uri. 

    Here is my code:

    Private Sub TextBox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TextBox.TextChanged
            WebView.Navigate(TextBox.Text)

    Please, use easy to understand language lol.

    After typing a URL in the TextBox and pressing Enter this code only validates the TextBox contains text before it uses a function to check the URL against your Domain Name Server. If the URL doesn't exist the try/catch statement is necessary as the code "Dim host As IPHostEntry = Dns.GetHostEntry(TBox1Text)" will crash the app because the TextBoxs text is not a valid URL. I didn't spend the time to catch the proper sockets exception but it works good enough for me.

    Also in your code I see you are using TextBox which is a a keyword (I think that's what its called) for the control TextBox. You should not use keywords for actual names of controls or anything else. Don't use Form for a Forms name or PictureBox for a PictureBoxs name, etc. Or Dim Integer as Integer or anything like that. Use Form1 or PictureBox1 or TextBox1 or some other names.

    Option Strict On
    
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                If TextBox1.Text <> "" Then
                    If ValidateHostName(TextBox1.Text) = True Then
                        WebBrowser1.Navigate(TextBox1.Text)
                    Else
                        MessageBox.Show("TextBox1 contains an invalid URL." & vbCrLf & "Please enter a valid URL in the TextBox and try again.")
                    End If
                End If
            End If
        End Sub
    
        Private Function ValidateHostName(ByRef TBox1Text As String) As Boolean
            Dim Value As Boolean = False
            Try
                Dim host As IPHostEntry = Dns.GetHostEntry(TBox1Text)
                Dim ip As IPAddress() = host.AddressList
                If ip.Count = 0 Then
                    Value = False
                Else
                    Value = True
                End If
            Catch ex As Exception
            End Try
            Return Value
        End Function
    
    End Class



    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Wednesday, January 1, 2014 1:57 AM 5555
    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:42 AM
    Wednesday, January 1, 2014 1:53 AM

All replies

  • Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim uriString As String = "ylabnez.xsb/retddg"
            'Dim uriString As String = "http://www.google.com"
    
            If Uri.IsWellFormedUriString(uriString, UriKind.Absolute) Then
                Dim MyURI As Uri = New Uri(uriString)
                MessageBox.Show("Success converting " & uriString & " to a valid URI", "All is well", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Cannot convert " & uriString & " to a valid URI", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub
    End Class
    try as is and then comment out the nonsense on and uncomment the google one
    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:40 AM
    Tuesday, December 31, 2013 11:55 PM
  • The error message I got: The value of type string cannot be converted to system.uri. 

    What type of object is WebView?  If it's a WebBrowser object then this is the method you are using. 
    http://msdn.microsoft.com/en-us/library/40x214wa(v=vs.110).aspx

    A string should be a valid type for the argument for this method - if you are getting an error then perhaps the string that is being passed is not a valid uri.  You can do the conversion explicitly using
            WebView.Navigate(New Uri(TextBox1.Text))

    To test if the string can be converted to a valid uri then see here:
    http://msdn.microsoft.com/en-us/library/ms131572(v=vs.110).aspx
    http://msdn.microsoft.com/en-us/library/system.uri.trycreate(v=vs.110).aspx

    Th

    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:40 AM
    Wednesday, January 1, 2014 12:00 AM
  • Hi,

     I just wanted to add that you can`t do this in the TextBox1.TextChanged event. Every time you type a character in the textbox or remove a character from it then the TextBox1.TextChanged event is triggered and it tries to navigate to the webpage and if you don`t have the URL all typed in then it will give you an error.

    For example say i want to go to "www.Google.com". As soon as i type the first "w" it will trigger the TextChanged event and when the WebBrowser tries navigating to "w" it will throw an error.

    Also you should name your textbox to something that will identify it like "TextBox_URL" or something so that you can easily tell which textbox is for what.


    • Edited by IronRazerz Wednesday, January 1, 2014 1:20 AM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:42 AM
    Wednesday, January 1, 2014 1:18 AM
  • I am building a simple web browser in VB 2013, in Windows 8.1. I used a video on youtube to guide me through the process of creating this browser. 

    When my coding was done, I pressed Local Machine and got a build error. The error is located in the area where the code wants the WebView to Navigate to a URL via TextBox.text. The error message I got: The value of type string cannot be converted to system.uri. 

    Here is my code:

    Private Sub TextBox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TextBox.TextChanged
            WebView.Navigate(TextBox.Text)

    Please, use easy to understand language lol.

    After typing a URL in the TextBox and pressing Enter this code only validates the TextBox contains text before it uses a function to check the URL against your Domain Name Server. If the URL doesn't exist the try/catch statement is necessary as the code "Dim host As IPHostEntry = Dns.GetHostEntry(TBox1Text)" will crash the app because the TextBoxs text is not a valid URL. I didn't spend the time to catch the proper sockets exception but it works good enough for me.

    Also in your code I see you are using TextBox which is a a keyword (I think that's what its called) for the control TextBox. You should not use keywords for actual names of controls or anything else. Don't use Form for a Forms name or PictureBox for a PictureBoxs name, etc. Or Dim Integer as Integer or anything like that. Use Form1 or PictureBox1 or TextBox1 or some other names.

    Option Strict On
    
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                If TextBox1.Text <> "" Then
                    If ValidateHostName(TextBox1.Text) = True Then
                        WebBrowser1.Navigate(TextBox1.Text)
                    Else
                        MessageBox.Show("TextBox1 contains an invalid URL." & vbCrLf & "Please enter a valid URL in the TextBox and try again.")
                    End If
                End If
            End If
        End Sub
    
        Private Function ValidateHostName(ByRef TBox1Text As String) As Boolean
            Dim Value As Boolean = False
            Try
                Dim host As IPHostEntry = Dns.GetHostEntry(TBox1Text)
                Dim ip As IPAddress() = host.AddressList
                If ip.Count = 0 Then
                    Value = False
                Else
                    Value = True
                End If
            Catch ex As Exception
            End Try
            Return Value
        End Function
    
    End Class



    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Wednesday, January 1, 2014 1:57 AM 5555
    • Proposed as answer by Cor Ligthert Wednesday, January 1, 2014 1:34 PM
    • Marked as answer by Carl Cai Monday, January 6, 2014 5:42 AM
    Wednesday, January 1, 2014 1:53 AM