3

I gave a byte[] stored in database,

I get the byte array in my DataTable from sql

Image

this is my DataTable, System.byte[] is my image in bytes which is stored in my datatbase

now I want to convert this DataTable into list

this is my current Code

var answerList = (from rw in dt.AsEnumerable()
                    select new RegistrationAnswers()
                    {
                        responseID = rw["responseID"].ToString() == string.Empty ? 0 : Convert.ToInt32(rw["responseID"].ToString()),
                        responseRegID = rw["responseRegID"].ToString() == string.Empty ? 0 : Convert.ToInt32(rw["responseRegID"].ToString()), 
                        responseAnswer = rw["responseAnswer"].ToString(),
                        _ResponseDocument =  rw["responseDocument"], //here i want to validate if rw["responseDocument"] is null or not and if this is not null then assign the byte[] data to _ResponseDocument 
                        formID=Convert.ToInt32(rw["formID"])
                    }).ToList();

when I updated my code to

//At top
byte[] tempByteArray = new byte[0];

_responseDocument = Convert.IsDBNull((byte[])rw["responseDocument"]) == false ? tempByteArray : (byte[])rw["responseDocument"],

I am getting following error

'Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.'

i want to validate if rw["responseDocument"] is null or not and if this is not null then assign the byte[] data to _ResponseDocument

3
  • What should _ResponseDocument be if the byte data is null ? Commented Oct 18, 2018 at 10:54
  • _ResponseDocument = (rw["responseDocument"]==null)?null:LoadBytesSomeHowFor(rw) Commented Oct 18, 2018 at 10:55
  • You seem to be checking whether IsDBNull is false and assigning a tempByte array if it is false i.e. if it's not null, you're assigning the tempByteArray. This seems to be the wrong way round Commented Oct 18, 2018 at 11:18

2 Answers 2

2

Try casting

rw["responseDocument"] == System.DBNull.Value ? new byte[0] : (byte[])rw["responseDocument"];

or

Convert.IsDBNull(rw["responseDocument"]) ? new byte[0] : (byte[])rw["responseDocument"];
Sign up to request clarification or add additional context in comments.

3 Comments

@stuartd check the question - the answer is apt, and almost certainly correct
for the OP's benefit: the problem here is that in the code in the question, you are casting to byte[] before checking for DBNull - the code shown here casts only after the DBNull check
If rw is a DataRow (which from surrounding code I suspect it is), it has an IsNull method that can be called rather than retrieving and comparing the DBNull value.
0

I faced the same problem while getting the data from DataTable into list

what I did is created a byte array in the method

byte[] tempByteArray = new byte[0];

and in my loop did something like this

_responseDocument = rw["responseDocument"].ToString() == "" ? tempByteArray : (byte[])rw["responseDocument"],

Comments

Your Answer

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