stringvb.netvb.net-2010dbnull

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


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


Solution

  • 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