I am trying to pass null
value to image database field if the ImageByteArray
is not provided to Parameters.Add
as shown here
cmd.Parameters.Add(new SqlParameter("@Img", SqlDbType.Image)).Value =
DBNull.Value ? null : ImageByteArray;
but I am getting error that says
Cannot implicitly convert type 'System.DBNull' to 'bool'
first is that correct way to do it >
if yes the how to pass null
value if the ImageByteArray
is not provided ?
i do not know if am right what i want to do is passing null to the parameter if the Byte Array is not provided so I avoid the Procedure or function expects parameter '@img', which was not supplied.
DBNull.Value ? null : ImageByteArray;
This part is wrong. Because ternary operator needs to be a boolean result to compare. Try the following one;
(object)ImageByteArray ?? DBNull.Value;
If ImageByteArray is null it will assign DBNull.Value to your sql parameter.
Here's what I'm using:
command.Parameters.Add("@Img", SqlDbType.VarBinary, ImageByteArray == null ? -1 :
ImageByteArray.Length).Value = ImageByteArray ?? (object)DBNull.Value;
Basically, when you send your byte array to your database, you need to specify if it is empty or not. Sending -1 for the length means the received array will be empty.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.