0

I try to download files from gridview .. i save files in database and then i display in gridview i try this

but this shows an error

An exception of type 'System.InvalidCastException' occurred in DecibelCRM.dll but was not handled in user code

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

on this line

 Dim binary() As Byte = DirectCast(structDb.dstResult.Tables(0).Rows(i).Item("document"), Byte())

my sp result look like this

documentid  documentname     DOCUMENT                          Docextension
43         Employment .pdf       0x255044462D312E330D25E2E3...        .pdf 
614       Information.xlsx   0xFFD8FFE12FFE4578696600004D...          .xlsx
615         pdf.pdf          0x504B0304140006000...                   .pdf
616        IMG_0207.JPG      0x456dfghs70004....                       .jpg
cool user
  • 59
  • 8
  • I think you are converting the wrong column to a `Byte` Array. You should be converting the column `DOCUMENT` not `documentname`. – Ahmad Jan 17 '17 at 06:49

1 Answers1

0

Use TryCast instead of DirectCast to avoid this issue

 Dim binary() As Byte = _
    TryCast(structDb.dstResult.Tables(0).Rows(i).Item("documentname"), Byte())

For more information why TryCast should be used here: Why use TryCast instead of DirectCast?

Community
  • 1
  • 1
Ahmad
  • 12,336
  • 6
  • 48
  • 88