DBNull to type string error...

lidds

Well-known member
Joined
Nov 9, 2004
Messages
210
Hi ya guys,

Cant seem to figure this one out, would not mind some help from the experts :p

Code:
                Do While myReader.Read
                    If (myReader.Item("Disp")) <> "Admin" Then
                        Dim dateCol = New DateTimeColumn(myReader.Item("Disp"))
                        dateCol.EditorStyle = EditorStyle.DropDown
                        dateCol.FormatString = "dd/MM/yyyy"
                        Me.lstDates.Columns.Items.AddRange(New Column() {dateCol})

                        For j = 0 To dsDates.Tables("dates").Rows.Count - 1                            
                            If Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "To")) Is DBNull.Value Then
                                strDateTo = Nothing
                            Else
                                strDateTo = Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "To"))
                            End If
                            If Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "From")) Is DBNull.Value Then
                                strDateFrom = Nothing
                            Else
                                strDateFrom = Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "From"))
                            End If
                        Next
                    End If
                Loop

Its giving me an error of "Cast from type DBNull to type String is not valid." on the following line.

Code:
If Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "To")) Is DBNull.Value Then

I am using a third party of software incase some of the code look strange.

Thanks inadvance

Simon
 
Change this:

If Trim(dsDates.Tables("dates").Rows(j).Item((myReader.Item("Disp")) & "To")) Is DBNull.Value Then

To:
Dim myVal as Object = dsDatas.Tables("dates").Rows(j).Item((myReader.Item("Disp)) & "To))
If Not myVal Is DBNull.Value Then
Your code here
End If

You cant trim a DBNull value.
 
Back
Top