0

Code is working but updated Unicode words show ??????? mark. How to fix this failure

conn = New MySqlConnection("server=" & server & ";user=" & user & ";pw=" & password & ";database=" & database & ";")
conn.Open()
       Dim cmd As New MySqlCommand("UPDATE product SET name = @name WHERE inventory_no = @inventory_no;", conn)
       cmd.Parameters.AddWithValue("@name", name_tb.Text)
       cmd.Parameters.AddWithValue("@inventory_no", inventoryno_tb.Text)
       cmd.ExecuteNonQuery()
conn.Close()
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Sandun
  • 101
  • 1
  • 5
  • Just so you know: [AddWithValue is Evil](http://www.dbdelta.com/addwithvalue-is-evil/), [AddWithValue is evil!](http://chrisrickard.blogspot.com/2007/06/addwithvalue-is-evil.html), and [Can we stop using AddWithValue() already?](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/). – Andrew Morton Jan 26 '20 at 15:39
  • See "question marks" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Jan 27 '20 at 01:12

1 Answers1

0

Finally, I got answer. Inserting the character set to the Connection String

   Character Set=utf8;

Like this

Public Function Mysqlconnection() As String

    conn = New MySqlConnection("server=" & server & ";user=" & user & ";pw=" & password & ";database=" & database & ";Character Set=utf8;")
    Return True

End Function
Sandun
  • 101
  • 1
  • 5
  • 1
    It is a good idea to use the [MySqlConnectionStringBuilder Class](https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlConnectionStringBuilder.htm) to build connection strings - it will take care of escaping anything that needs it. Note that database connections should have `.Dispose()` called on them as soon as the code has finished the current operation - you should not use just one connection for the life of the program. – Andrew Morton Jan 26 '20 at 15:44
  • I notice that the function returns the wrong type (a Boolean) instead of the declared String. An easy way to get Visual Studio to point out errors like that for you is to use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360). – Andrew Morton Jan 26 '20 at 15:46