title: handling dbnull data in vb.net
body: <p>I want to generate some formatted output of data retrieved from an MS-Access database and stored in a <em>DataTable</em> object/variable, myDataTable. However, some of the fields in myDataTable cotain <em>dbNull</em> data. So, the following VB.net code snippet will give errors if the value of any of the fields <em>lastname</em>, <em>intials</em>, or <em>sID</em> is <em>dbNull</em>.</p>
  
  <pre><code>   dim myDataTable as DataTable
     dim tmpStr as String
     dim sID as Integer = 1
  
     ...
     myDataTable = myTableAdapter.GetData() ' Reads the data from MS-Access table
     ...
  
     For Each myItem As DataRow In myDataTable.Rows
  
      tmpStr = nameItem("lastname") + " " + nameItem("initials")
  
      If myItem("sID")=sID Then
          ' Do something
      End If
  
      ' print tmpStr
  
     Next
  </code></pre>
  
  <p>So, how do i get the above code to work when the fields may contain <em>dbNull</em> without having to check each time if the data is dbNull as in <a href="https://stackoverflow.com/questions/105671/any-systemdbnullvalue-vs-any-is-systemdbnull">this question</a>?</p>