0

I'm trying to build a Windows Universal App with a MySql database. My problem is, that when i try to do a SELECT command, I get this error: 'windows-1252' is not a supported encoding name.'

I'm usining MySQL Connector Net 6.9.8 and Visual Studio 2015.

There is a realted post wich has exactly the same problems like me: Windows -1252 is not supported encoding name. C#

But I'm a beginner with universal apps. During my resarch I also found this post: Reading Windows-1252 encoding in Windows Phone 8, in the first command, there is al link to a manual encoder. But as I told you I am a beginner and so I didn't get the solution and also have no idea how to use this for my app. Please explain for an beginner ;)

EDIT: At first I got the same porblem with the INSERT command, but I could fix it with setting charset=utf8 in the connection. But this doesn't work with the SELECT command.

Here is the cod of my App:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using SQLite;
using MySql;
using MySql.Data.MySqlClient;

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();
    }


    #region ++++++++MySql++++++++
    static string server = "127.0.0.1";
    static string database = "test";
    static string user = "root";
    static string pswd = "";
    MySqlConnection connectionMySql;
    int id = 1;

    private void MySqlConnectButton_Click(object sender, RoutedEventArgs e)
    {
        MySqlStatusTextBlock.Text = "Connecting...";
        string connectionString = "Server = " + server + ";database = " + database + ";uid = " + user + ";password = " + pswd + ";SslMode=None;charset=utf8";
        connectionMySql = new MySqlConnection(connectionString);
        connectionMySql.Open();
        MySqlStatusTextBlock.Text = "Connected";
    }

    private void MySqlAddButton_Click(object sender, RoutedEventArgs e)
    {
        Foods food = new Foods() { Name = MySqlElementA.Text, Keyword = MySqlElementB.Text, Calories = int.Parse(MySqlElementC.Text), Fat = double.Parse(MySqlElementD.Text), Carbs = 33.4, Protein = 24.8 };

        string para = "('" + id + "','" + food.Name + "','" + food.Keyword + "','" + food.Calories + "','" + food.Fat + "','" + food.Carbs + "', '" + food.Protein + "') ";
        string commandMySql = "INSERT INTO `foods`(`id`, `name`, `keyword`, `calories`, `fat`, `carbs`, `protein`) VALUES" + para;
        MySqlCommand cmd = new MySqlCommand(commandMySql, connectionMySql);

        MySqlDataReader dr;
        dr = cmd.ExecuteReader();

        int count = 0;
        while (dr.Read())
            count += 1;
        if (count == 0)
        {
            MySqlStatusTextBlock.Text = "Added";
        }
        else
        {
            MySqlStatusTextBlock.Text = "Error";
        }
        dr.Close();

    }

    private void MySqlShowAllButton_Click(object sender, RoutedEventArgs e)
   {
        List<Foods> f_list = new List<Foods>();
        string query = "SELECT * FROM `foods` WHERE 1 ";
        MySqlCommand cmd = new MySqlCommand(query, connectionMySql);

        MySqlDataReader dr;
        dr = cmd.ExecuteReader(); **<--- Here occurs the error**

        while (dr.Read())
        {
            Foods f_get = new Foods() {ID = dr.GetInt32("id"), Name = dr.GetString("name"), Keyword =dr.GetString("keyword"), Calories = dr.GetInt32("calories"), Fat = dr.GetDouble("fat"), Carbs = dr.GetDouble("cabrs"), Protein = dr.GetDouble("protein")};
            f_list.Add(f_get);
        }
        string textbox = String.Empty;
        foreach(Foods f in f_list)
        {
            textbox = String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", f.ID.ToString(), f.Name, f.Keyword, f.Calories, f.Fat, f.Carbs, f.Protein) + System.Environment.NewLine;
        }
        MySqlResultTextBlock.Text = textbox;

    }
    #endregion


}
Community
  • 1
  • 1
Max
  • 1,368
  • 4
  • 18
  • 43

2 Answers2

1

You can make changes in database, not on the client side.
Check your database collation
You can try convert database and table collaction with:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

PS: don't forget to make a backup before conversion.

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25
0

I didn't worked with only setting the collation to utf8,

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

but in in your link Collation the first command get it work. So thank you very much! Here is how i get it fixed:

If you use PHP with mysql you may want to use in my.ini: skip-character-set-client-handshake with:

collation_server=utf8_unicode_ci
character_set_server=utf8
Max
  • 1,368
  • 4
  • 18
  • 43