title: vb.net DBNull to string error
body: <p>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'. </p>
  
  <p>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</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <pre><code>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]='" &amp; ListView1.Text &amp; "'", 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
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/6zisn.png" alt="enter image description here"></p>