Weird encoding behavior & Event-Loop

My situation:
I want to switch a webapp, which is already running with a SQLite DB, to MySQL.

I’ve made the connection, brought all the data from the SQLite DB to the MySQL DB and everything seems fine.

Now I’m about to rewrite my code to handle the MySQL specifications. One is the encoding. The Encoding of my DB and all of its tables and fields is UTF8.

I use this code to select a value :

         dim RS as RecordSet = Session.mDB.SQLSelect(QUERY)
          dim val as String = RS.IdxField(i).StringValue.DefineEncoding(Encodings.UTF8)

The error:
This all seems to work. But if I want to set the result into a textfield (for example) the following error occurs:

Unhandled RuntimeException
Message: The data could not be converted to text with this encoding.

Stack:
GenerateJSON
xojo.Data.GenerateJSON%y%x
WebResponse._Render%s%o<WebResponse>
WebSession._HandleEvent%%o<WebSession>so<_HTTPServer.HTTPRequestContext>
WebSession._HandleRequest%i4%o<WebSession>so<_HTTPServer.HTTPRequestContext>
WebApplication._HandleHTTPRequest%%o<WebApplication>o<_HTTPServer.HTTPRequestContext>
_HTTPServer.HTTPRequestThread.Event_Run%%o<_HTTPServer.HTTPRequestThread>
rbframework.dylib$1225
rbframework.dylib$1016

If I handle it over the unhandledexception-event, I get a loop-event-error.

What can I do???

Try this after connecting to the database:

Db.SQLExecute("SET NAMES utf8")

WOW!
It works!
Thank you!

But, what does this do?

The server doesn’t know anything about the encoding on the client side of a session. I assume that the server uses ASCII if not told otherwise, but I’m not sure.