I have an sql query that has a parameter that can be null in the database (Sql Server). The update method work fine until that user put a blank in the field, this produce a null value for the DataTime object (this object is nullable). The problem is when the dbCommand.ExecuteNonQuery();.
Here is how I build the parameter for this field:
IDataParameter dbParam_au_id = new SqlParameter();
dbParam_au_id.ParameterName = "@birthday";
dbParam_au_id.Value = birthday;
dbParam_au_id.DbType = DbType.DateTime;
dbCommand.Parameters.Add(dbParam_au_id);
I have try to convert the null value of birthday to DBNull.Value like that :
IDataParameter dbParam_au_id = new SqlParameter();
dbParam_au_id.ParameterName = "@birthday";
dbParam_au_id.Value = birthday??DBNull.Value;
dbParam_au_id.DbType = DbType.DateTime;
dbCommand.Parameters.Add(dbParam_au_id);
But this code won’t compile and I get error :
Error 1 Operator ‘??’ cannot be applied to operands of type ‘System.DateTime?’ and ‘System.DBNull’
Any idea?
The types are not compatible. Try something like this: