Conversion from type DBNull to type String is not valid in vb.net

334 Views Asked by At

Please the solution I have an error "Conversion from type 'DBNull' to type 'String' because the pathhelper column has a null value

Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
        If e.Column.FieldName = "Image" AndAlso e.IsGetData Then
            Dim view As GridView = TryCast(sender, GridView)

            Dim colorName As String = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Color"))
            Dim fileName As String = GetFileName(colorName).ToLower()
            ***Dim PATHHELPER As String = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper")) 'error this line***


            If (Not Images.ContainsKey(fileName)) Then
                Dim img As Image = Nothing
                Try
                    Dim filePath As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & PATHHELPER, fileName, False)
                    img = Image.FromFile(filePath)
                Catch
                End Try
                Images.Add(fileName, img)
            End If
            e.Value = Images(fileName)
        End If
    End Sub
End Class

viewdatabase

2

There are 2 best solutions below

0
Mary On BEST ANSWER

Use the DBNull class Value property for comparison.

Dim PATHHELPER As String
If Not DBNull.Value.Equals(View.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper")) Then
    PATHHELPER = CStr(View.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper"))
Else
    PATHHELPER = String.Empty 'or Nothing depending what comes next.
End If
3
Hursey On

Something like this I think should do the trick. Probably far better ways to do it but this is the way I would

Dim PATHHELPER as String = String.Empty
If view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper") <> DBNull.Value Then
    PATHHELPER = view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper").ToString
End if