[Solved] VB.Net Error: Unable to cast object of type System.DBNull to type System.Byte

makumbi
 
on Jul 14, 2022 11:37 PM
568 Views

 Please help i am getting the error below when i try to open page

Line 25:         If e.Row.RowType = DataControlRowType.DataRow Then
Line 26:             Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
Line 27:             Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("sign"), Byte()))
Line 28:             CType(e.Row.FindControl("Image1"), Image).ImageUrl = imageUrl
Line 29:         End If

error

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 14, 2022 11:44 PM

Hi makumbi,

You need to check the DataRowView Item value with IsDBNull.

If its not Null then only set the ImageUrl by converting to ToBase64String.

Use the modified code.

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
    If Not IsDBNull(dr("sign")) Then
        Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("sign"), Byte()))
        CType(e.Row.FindControl("Image1"), Image).ImageUrl = imageUrl
    End If
End If