Forum Discussion

_Nexthop_'s avatar
_Nexthop_
Copper Contributor
Jan 15, 2025

Mysql connection error

Hello,

I need help with a connection with C#.

I have a database that I just dumped, the connection with the mysql.data tools was working perfectly, but after the dump I get this error:

Unable to cast object of type 'System.DBNull' to other types

   in System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
   in System.Convert.ToInt32(Object value, IFormatProvider provider)
   in MySql.Data.MySqlClient.Driver.<LoadCharacterSetsAsync>d__81.MoveNext()
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in MySql.Data.MySqlClient.Driver.<ConfigureAsync>d__78.MoveNext()
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   in MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__94.MoveNext()
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in MySql.Data.MySqlClient.MySqlConnection.Open()
   in DBTester.FormMain.btnTest_Click(Object sender, EventArgs e) in C:\Users\...

 

The absurd thing is that if I try several times, on the fourth or fifth attempt it succeeds, has anyone ever encountered this problem, or can they help me figure out what may have gone wrong?

Now DB works on MariaDB 11, the old db was on MariaDB 8

 

What can I verify?

2 Replies

  • UdhayarajanJ's avatar
    UdhayarajanJ
    Copper Contributor

    Hello! 

    That error usually appears when you're trying to read a column from a database result that has a NULL value (i.e., DBNull.Value) and trying to cast it directly to another type (like int, string, DateTime, etc.) without checking if it's null first.

    Go to the line where the exception is thrown.

    Identify the column being accessed from the reader (or data row).

    Add a DBNull check before casting or converting.

  • wuchen9527's avatar
    wuchen9527
    Copper Contributor

    你检查看看你的数据,字段为int类型的是不是被解析成空值了

Resources