Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

VS 2010 [RESOLVED] Conversion from type 'DBNull' to type 'String' is not valid. How to handle properly.

$
0
0
Hi,
I have ALL my catch blocks writing to a log file that gets sent to me once a day.
I see the error:
Title: Error
Message: Conversion from type 'DBNull' to type 'String' is not valid.
StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value)
at TenantTracker2013.TenantList.TenantsDataGridView_CellFormatting(Object sender, DataGridViewCellFormattingEventArgs e)
Date/Time: 1/12/2014 8:03:17 AM

I'm not quite sure how to get rid of it.
Its a simple form with a datagridview on it, with several columns, one being the birthday, I also have an Age column that I use CellFormatting to calculate the age.
I believe the error occurs when the birthday field is empty, I thought the following code would prevent the error, but it does not. So the cell formatting is for any column? That being the case, I have 2 other date fields in the datagridview that could also be empty/null, maybe its not my birthday field causing the problem.
Code:

Private Sub TenantsDataGridView_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles TenantsDataGridView.CellFormatting
        Try

            If e.RowIndex <> -1 AndAlso e.ColumnIndex = Birthday1.Index Then
                If String.IsNullOrEmpty(e.Value) = False Then
                    Dim dob As Date = e.Value
                    TenantsDataGridView.Rows(e.RowIndex).Cells("Age1").Value = CStr(Math.Floor(Now.Year - dob.Year))
                End If
            End If
        Catch ex As Exception
            Dim el As New ErrorLogger
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")
        End Try
    End Sub

Thanks
LB

Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>