0

Dear Experts
I use following codes to retrieve data from table, but get error

Conversion from type 'DBNull' to type 'String' is not valid.

str = "Select * from gpass where vou_no= " & Val(Me.txtGat.Text) & " And Date =   '" & Me.txtDat.Text & "'"
        dt = GetTable(str)

        If (dt.Rows.Count > 0) Then


            DataGridView1.Rows.Clear()

            For i = 0 To dt.Rows.Count - 1

                Me.DataGridView1.Rows.Add()
                Me.DataGridView1.Rows(i).Cells(1).Value = IIf(IsDBNull(dt.Rows(i)("tittle")), " ", Trim(dt.Rows(i)("tittle")))
            Next
        End If
End If

Tittle column of gpass is as

Column------datatype
Tittle--------char(100)

And data in column is as
Null
Null
Null


Please help

3
Contributors
2
Replies
18
Views
7 Years
Discussion Span
Last Post by sknake
0

Trim(value) is being evaluated before IIF(IsDBNull()) and is causing the exception.

I would suggest altering your code to something like this:

Dim temp as String = IIf(IsDBNull(dt.Rows(i)("tittle")), " ", dt.Rows(i)("tittle"))
Me.DataGridView1.Rows(i).Cells(1).Value = Trim(temp)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.