I have simple connectionstring to MySql (MariaDB 5.5.5-10.11.0) written in c#:
MySqlConnection Database = new MySqlConnection("Server=127.0.0.1; Port=3306; Database=test; Uid=user; Pwd=MyPassword; Ssl Mode=Required; convert zero datetime=True;");
Everything works fine on two computers (Windows 10 and Windows 11). But when I try to launch this app on Windows Server 2022 I get this error:
System.InvalidCastException: Object cannot be cast from DBNull to other types.
at System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value, IFormatProvider provider)
at MySql.Data.MySqlClient.Driver.LoadCharacterSets(MySqlConnection connection)
at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MariaDB.Program.StartAPI()
Error is thrown on Database.Open();
MariaDB is installed and running, Ssl is working, user's pemissions are granted, port is correct. Any ideas please?
Whole program:
using System;
using MySql.Data.MySqlClient;
namespace MariaDB
{
internal class Program
{
MySqlConnection Database = new MySqlConnection("Server=127.0.0.1; Port=3306; Database=test; Uid=user; Pwd=MyPassword; Ssl Mode=Required; convert zero datetime=True;");
static void Main(string[] args)
{
Program p = new Program();
p.OpenDB();
}
private void OpenDB()
{
Database.Open();
Console.WriteLine("Ok");
Console.ReadLine();
}
}
}
Database.Open();
. Opening a database connection is not a cast and does not involve other types so your error is very, very likely elsewhere – HeadonLoadCharacterSets
is being called when the connection is opened the first time. It's exectingSHOW COLLATION
and reading the record set. Apparently,. it's getting aNULL
for theid
column,, andConvert.Int32
is throwing because of that. If you execute that statement in a query tool, what do you get (please edit the question and add the record set)? – Gridley