Bug #90149 NET.Core C# MySqlParameter value returns null
Submitted: 20 Mar 2018 19:29 Modified: 20 Apr 2018 6:16
Reporter: Karl Krieger Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.10.6 OS:Microsoft Windows (Windows 10 64 bit)
Assigned to: CPU Architecture:Any
Tags: C#, core, MySQL, MySqlParameter, NET

[20 Mar 2018 19:29] Karl Krieger
Description:
MySqlParameter value is assigned but still returns null when read. This happens if value which was assigned is local constant.

How to repeat:
Here's a c# code to replicate the problem (Read comment descriptions below).

static void Main(string[] args){          
    Test();
}

private static void Test() {
    uint value_a = 0;
    const uint value_b = 0; //This is the failing case

    MySqlParameter a = new MySqlParameter("a", value_a);
    MySqlParameter b = new MySqlParameter("b", value_b);

    DebugTest(a);
    DebugTest(b);
}

private static void DebugTest(MySqlParameter par) {
    string name = par.ParameterName == null ? "NULL" : par.ParameterName;
    string value = par.Value == null ? "NULL" : par.Value.ToString();

    Console.WriteLine(string.Format("name: {0}  value: {1}", name, value));

    //Expected debug for both entries is:
    //    "a"  0
    //    "b"  0

    //Resulting debug we get
    //    "a"  0
    //    "b"  NULL  <---*ERROR*
}
[21 Mar 2018 10:53] Karl Krieger
Additional Info
    * Visual Studio 2017 community edition
    * Net core 2.0 c# application
    * I added library Mysql.Data (6.10.6) using NuGet
[21 Mar 2018 18:25] Bradley Grainger
This looks like a C# compiler overload resolution problem, not necessarily a Connector/NET issue (although it's facilitated by problematic API design).

This code invokes the MySqlParameter(string parameterName, MySqlDbType dbType) overload, with an argument value of MySqlDbType.Decimal (== 0).

MySqlParameter b = new MySqlParameter("b", value_b);

A workaround is to explicitly invoke the MySqlParameter(string parameterName, object value) overload:

MySqlParameter b = new MySqlParameter("b", (object) value_b);

I don't know why MySqlDbType is a better overload resolution match for a const uint. You could ask on Stack Overflow (or file a bug on Roslyn but I suspect it's either by design or a long-standing decision that can't be changed for backwards compatibility reasons).
[21 Mar 2018 19:21] Karl Krieger
Interesting, wonder why not introduce overloads for basic primitive types, as db itself is strongly typed.
Same like for MySqlDataReader.GetUInt() leaving option for developer to cast manually MySqlDataReader["name"], that would also save useless casting to object and back (if its being re-resolved from passed object type).
[20 Apr 2018 6:16] Chiranjeevi Battula
Hello Karl Krieger,

Thank you for the bug report.
Verified based on internal discussion with dev's.

Thanks,
Chiranjeevi.