Resolved Windows -1252 is not supported encoding name

DavLn

Member
Joined
Mar 31, 2023
Messages
13
Programming Experience
Beginner
Hello,

I get an encoding related error when I want to check if my database connection is working when I press the "Connection" button on my form.
The error is on "conn.Open();".
I have attached my static class which contains the function for connection and the code where I call my function "Connection()"

I hope I can get help from you if possible.
C#:
using System;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;



namespace GestionHotel
{
    public static class Fonctions
    {
        private static String cs = @"server=localhost;userid=root;password=;database=gestionhotelentretien;charset=utf8";
        private static MySqlConnection conn = new MySqlConnection(cs);
    

        public static bool Connexion()
        {
            conn.Open();
            if (conn.State == System.Data.ConnectionState.Open)
            {

                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Deconnexion()
        {
            conn.Close();
            return (conn.State == System.Data.ConnectionState.Closed);
        }
    }
}
 
I've already looked at this article, I had no problem installing the package using Nuget or adding the reference but the problem is that I don't know where to put this: Encoding.RegisterProvider(CodePagesEncodingProvider. Instance);
 
I would also try moving the "new MySqlConnection" to after the code page has been initialized.
 
The line 16 consist of two parts:
  1. declaring the field conn of type MySqlConnection
  2. creating a MySqlConnection instance and assigning it to the field (this is actually two parts as well, creating and assigning...)
You can change line 16 to only do part 1, and then do part 2 for example around line 21 before you open the connection, of course also do that code page thing first.
 
I did it but I still get the same error... It's too much I can't take it anymore I tried a lot of things but in the end no result...

C#:
private static MySqlConnection conn = null;
//part 2
conn = new MySqlConnection(cs);
 
I have no other suggestions, there may be a problem with the database you're connecting to.
 
Try putting that registration in your Main() method as the first line.
 
In the end, the problem did not come from my code but rather because I did not install the following package :

packageNuget.PNG


I installed it via Nuget, and thank you for all the help you gave me!

Have a nice day !
 
I thought the MySql types you were using came from that package?
 

Latest posts

Back
Top Bottom