Dear Friends,

I have been working on a VB.net project on VB8 with ms access as backend.

While finding certain data by entering primary key in the form i.e. id i experienced a problem. While displaying the values which are null in the database like wise tel1 is null then it shows the values in the form up to it and does not show the values after it. When there is null or empty value then the dbnull exception is shown. my code has been given below:

Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
 tmp.Delete()
        B2.Enabled = False
        B1.Enabled = True
        MsgBox("Record is deleted")
End Sub

Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click
    Dim s As String


    con.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=K:\Database\db1.mdb"
    con.Open()

    s = "select * from add1 where ID='" & T18.Text & "'"
    tmp.Open(s, con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    If tmp.EOF = True And tmp.BOF = True Then
        MsgBox("No record available")
        T18.SelectAll()
        T18.Focus()
        tmp.Close()
        Exit Sub
    End If
    B2.Enabled = True
    T1.Text = tmp.Fields("Salutation").Value
    T1.Focus()

    T2.Text = tmp.Fields("Fname").Value
    T2.Focus()
    T3.Text = tmp.Fields("Mname").Value
    T3.Focus()
    T4.Text = tmp.Fields("Lname").Value
    T4.Focus()
    T5.Text = tmp.Fields("Degree").Value
    T5.Focus()
    T6.Text = tmp.Fields("Category").Value
    T6.Focus()
    T17.Text = tmp.Fields("Position").Value
    T17.Focus()
    T16.Text = tmp.Fields("Org").Value
    T16.Focus()
    T7.Text = tmp.Fields("Street").Value
    T7.Focus()
    T8.Text = tmp.Fields("City").Value
    T8.Focus()
    T9.Text = tmp.Fields("State").Value
    T9.Focus()
    Cmb1.Text = tmp.Fields("Country").Value
    Cmb1.Focus()
    T10.Text = tmp.Fields("Tel1").Value
    T10.Focus()
    T11.Text = tmp.Fields("Tel2").Value
    T11.Focus()
    T12.Text = tmp.Fields("Tel3").Value
    T12.Focus()
    T13.Text = tmp.Fields("Fax").Value
    T13.Focus()
    T14.Text = tmp.Fields("Email").Value
    T14.Focus()
    T15.Text = tmp.Fields("URL").Value
    T15.Focus()


    B1.Enabled = False
    B2.Enabled = True
End Sub

when i debug the program then I recieve following runtime error after clicking the find button

InvalidCastException was unhandled

which shows

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

Please help me how can I solve the problem.
Can i recieve appropriate code for it.

Sincerly yours
Kshiteesh Vaskota
BIM 8th semester
Nagarjuna College

Edited 3 Years Ago by pyTony: fixed formatting

You can use the ToString() method of the value and it will turn DBNull into an empty string.

T7.Text = tmp.Fields("Street").Value.ToString()

Or you can test for DBNull and just not set the text property if it's true.

If tmp.Fields("Street").Value <> DBNull.Value Then 
  T7.Text = tmp.Fields("Street").Value
End If
This question has already been answered. Start a new discussion instead.