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

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)

You can call Convert.ToString() on DBNull.Value and it will return string.Empty .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.