I have a sub which just adds the values from a access db to textboxes. However, some of the fields in the db contain null values or are empty and this is causing vb to throw an error, 'Cannot convert from DBNull to string'.
How can I fix this based on my posted code or if someone could post a tutorial for this type of event, as I am a new user struggling to make sense of this. Many thanks
Sub add()
While dr.Read()
txtname.Text = dr(0).ToString()
txtfathername.Text = dr(1).ToString()
txtaddress.Text = dr(2).ToString()
txtemail.Text = dr(3).ToString()
End While
End Sub
UPDATE:
Sub filllistview()
Try
'creatconn()
cn.Open()
Dim cmd As OleDbCommand = New OleDbCommand("Select * from Postings", cn)
dr = cmd.ExecuteReader()
While dr.Read()
ListView1.Items.Add(dr(0).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3))
End While
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
dr.Close()
cn.Close()
End Try
End Sub
Sub showcontectsinlistview()
Try
'creatconn()
cn.Open()
Dim cmd As OleDbCommand = New OleDbCommand("select * from Postings where [Code]='" & ListView1.Text & "'", cn)
dr = cmd.ExecuteReader()
add()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
dr.Close()
cn.Close()
End Try
End Sub