Unknown User Unknown User - 1 year ago 109
C# Question

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

I'm using

c# 4

My Procedure parameter is

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;

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

Answer Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download