Search code examples
c#mysqlstored-proceduresnvarchardbnull

How to pass DBNull in Nvarchar type(procedure paramter) in MySql


I'm using MySQL c# 4

My Procedure parameter is NVARCHAR(100).

So what I'm doing is

MySqlParameter DBParam1 = new MySqlParameter("var_Name", (object)(string.IsNullOrEmpty(Name)? DBNull.Value : Name));

MySqlCommand _DBCommand = new MySqlCommand();
_DBCommand.Connection = _DBConnection;
_DBCommand.CommandText = "udsp_ProcedureName";
_DBCommand.CommandType = System.Data.CommandType.StoredProcedure;
_DBCommand.Parameters.Clear();
_DBCommand.Parameters.Add(DBParam1);

so this is throwing "ErrorMessage":"Unhandled type encountered"

How can i solve this?

what i looked into

How do I Parameterize a null string with DBNull.Value clearly and quickly

Is there a more elegant form for assigning NULL to InsertCommand's NVarChar?

Exception when AddWithValue parameter is NULL


Solution

  • Passing DBNull.Value to NVARCHAR type will throw an exception

    "ErrorMessage":"Unhandled type encountered"

    So I passed this as Empty string.

    MySqlParameter DBParam1 = new MySqlParameter("var_Name", (object)(string.IsNullOrEmpty(Name)? string.Empty : Name));