title: How to deal with SqlDataReader null values in VB.net
body: <p>I have the follwoing code that performs a query and returns a result. However, I looked around and found some examples to take care of null values but I get an error: "Invalid attempt to read when no data is present." I also got the error: "Conversion from type 'DBNull' to type 'Decimal' is not valid."</p>
  
  <p>Can someone help me out with this code to prevent null values from crashing my program?</p>
  
  <pre><code>Private Sub EFFICIENCY_STACKRANK_YTD(ByVal EMPLOYEE As String)
  
      Dim queryString As String = "SELECT " &amp; _
      " (SELECT CAST(SUM(TARGET_SECONDS) AS DECIMAL)/ CAST(SUM(ROUTE_SECONDS) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN WITH(NOLOCK) WHERE APE_AREA_OBJID = " &amp; lblAreaOBJID.Text &amp; " AND EMPLOYEE_NAME = '" &amp; EMPLOYEE &amp; "' AND YEAR_TIME = '" &amp; cbYear.Text &amp; "' AND ACTIVE = 1) AS RESULT1" &amp; _
      " FROM dbo.APE_BUSDRIVER_MAIN "
  
  
      Using connection As New SqlConnection(SQLConnectionStr)
          Dim command As New SqlCommand(queryString, connection)
          connection.Open()
          Dim reader As SqlDataReader = command.ExecuteReader()
  
          If reader.Read Then
              RESULT1 = reader("RESULT1")
          Else
              RESULT1 = 0
          End If
  
      End Using
  End Sub
  </code></pre>