0

I have this code :

UploadImageControl1.BinaryData =ServiceInfoDt["SERVICE_LOGO"]!=null?(byte []) ServiceInfoDt["SERVICE_LOGO"]:null;

BinaryData is a byte array byte[]

buy i am receiving this error :

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
Sora
  • 2,465
  • 18
  • 73
  • 146

1 Answers1

5

You should check with System.DBNull.Value instead of null

If a database field has missing data, you can use the DBNull.Value property to explicitly assign a DBNull object value to the field. However, most data providers do this automatically.

Thus use

UploadImageControl1.BinaryData = 
    ServiceInfoDt["SERVICE_LOGO"]!= System.DBNull.Value 
    ? (byte []) ServiceInfoDt["SERVICE_LOGO"]
    : null;

Read What is the difference between null and System.DBNull.Value?

Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168