2

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.

6
  • You aren't passing the value. You are trying to treat it as a boolean. You typed someCondition ? value1 : value2. Commented Sep 28, 2018 at 13:25
  • You're passing DBNull.Value to the ternary operator. That only accepts a boolean and DBNull.Value cannot be implicitly converted to a boolean. Commented Sep 28, 2018 at 13:25
  • 1
    You probably wanted to write ImageByteArray ?? DBNull.Value instead? Commented Sep 28, 2018 at 13:26
  • 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. Commented Sep 28, 2018 at 13:35
  • How is ImageByteArray declared? How is it passed in? If it's a byte[] can it really be null? Or just zero length? Commented Sep 28, 2018 at 13:43

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks but I got error cannot be applied to operands of type 'byte[]' and 'System.DBNull' and when I removed one ? sing i got missing :
IIRC for this to work, ImageByteArray has to be nullable, i.e. byte?[]
@sam can you share your full code? because byte[] is a nullable type and it shouldn't be cause any error. Maybe you might missing something?
@sam can you just put (object) before ImageByteArray for now? I will dig in and will reply with a more proper reply
@sam it's cmd.Parameters.Add(new SqlParameter("@Img", SqlDbType.Image)).Value = (object)ImageByteArray ?? DBNull.Value right ?
|
2

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.

1 Comment

Thanks for sharing your experience

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.