Display HTML string in WebView2 control with VB.NET causes error

36 views Asked by At

I am trying to build an HTML string which I then want to display in my WebView2 control which is called WebView2_1. The code is as follows:

Dim selectQuery As String = "SELECT name FROM chats;"
Using command As New SQLiteCommand(selectQuery, sqlite_conn)
    Using reader As SQLiteDataReader = command.ExecuteReader()
        ' Iterate through the records and display names in WebView2
        Dim namesHtml As New System.Text.StringBuilder()
        namesHtml.AppendLine("<html lang=""en"">")
        namesHtml.AppendLine("<meta charset=""UTF-8"">")
        namesHtml.AppendLine("<ul>")
        While reader.Read()
            namesHtml.AppendLine($"<li>{reader("name")}</li>")
        End While
        namesHtml.AppendLine("</ul>")
        WebView2_1.CoreWebView2.NavigateToString(namesHtml.ToString())
        'MsgBox(namesHtml.ToString())
    End Using
End Using

The error I am getting is on this line:

WebView2_1.CoreWebView2.NavigateToString(namesHtml.ToString())

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Microsoft.Web.WebView2.WinForms.WebView2.CoreWebView2.get returned Nothing.

Visual Studio screenshot

Do I need to initialise the control in some way or have I made another mistake?

Thanks very much

0

There are 0 answers