locked
How to check if an ordinals as a key that exist to prevent this error. Unable to cast object of type 'System.DBNull' to type 'System.Byte[] RRS feed

  • Question

  • User-1021989441 posted

    I have the following line of code that is giving me an error is I have a DBNull value. How will how go about in resolving this?

    byte[] pictureAsByteArray = (byte[])reader.GetValue(ordinals.GetOrdinal("xxxSignature"));

    Wednesday, October 23, 2019 12:20 AM

All replies

  • User-719153870 posted

    Hi yguyon,

    I have the following line of code that is giving me an error is I have a DBNull value. How will how go about in resolving this?

    This error is usually caused by a field value is DBNull and it can't be converted to byte[].

    To solve this problem, please check DBNull. Value Field and refer to below code:

    if (!DBNull.Value.Equals(reader.GetValue(ordinals.GetOrdinal("xxxSignature"))))
                {
                    byte[] pictureAsByteArray = (byte[])reader.GetValue(ordinals.GetOrdinal("xxxSignature"));
                }
                else
                {
                    byte[] pictureAsByteArray = new byte[0];
                }
            }

    Best Regard,

    Yang Shen

    Wednesday, October 23, 2019 2:03 AM