29

I am working with windows 10 universal App and the ARM CPU to create apps for the Raspberry Pi. I get the following error with encoding:

Additional information: 'windows-1252' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

 private async void Login(string passcode)
    {
        try
        {
            MySqlConnection conn = new MySqlConnection("Server=...");
            MySqlCommand cmd;

            conn.Open();

            cmd = new MySqlCommand("Select * from ...");

            var dr = cmd.ExecuteReader();

            int count = 0;

            while (dr.Read())
                count += 1;

            var dialog = new MessageDialog((count == 1) ? "Logged In" : "Error");
            await dialog.ShowAsync();

        }
        catch (Exception ex)
        {
           var dialog = new MessageDialog(ex.Message);
           await dialog.ShowAsync();
        }
        finally { conn.Close(); }
    }
}

I get the error in this line of code

dr = cmd.ExecuteReader();

Before I used to get it in

conn.open();

But I was able to solve it by adding

charset=utf8

to the connection string.

How can I solve this error?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Muhand Jumah
  • 291
  • 1
  • 3
  • 3

2 Answers2

51

From a .NET Core 2.2 project, I had to install via Nuget the following two packages:

System.Text.Encoding | CodePages

(System.Text.Encoding & System.Text.Encoding.CodePages)

Then you have to set it before the use of libraries:

 using System.Text;
 …
 {
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    ...
 }
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
38

I resolved this issue by adding

System.Text.EncodingProvider ppp = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(ppp);
bombek
  • 553
  • 1
  • 7
  • 23
  • 1
    This is correct anwser ! It help me fix my problem ! Thanks ! – Shinichi Dec 21 '20 at 02:06
  • 1
    As @ΩmegaMan noted, for .NET Core/Standard project you need to add NuGet package ```System.Text.Encoding.CodePages``` to references. – mikiqex Apr 03 '21 at 11:53