title: Invalid attempt to read data even when data is present in SqlDataReader in vb.net
body: <p>Here's my code which gives error, the query returns value for the particular item.</p>
  
  <p>Also in the database side the query return rows even I have put condition that if reader has rows then only assign it to a variable but still it throws an error eg. </p>
  
  <pre><code>dqty = sqlreader("qty")
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>Private Function checkquantity(ByVal code As String, ByVal quan As Integer) As Boolean
      sqlcommand.CommandText = "select sum(qty) as qty from pos_stock_balance where item_code='" &amp; code &amp; "'"
      sqlcommand.Connection = AppsCon
      sqlreader = sqlcommand.ExecuteReader
      If sqlreader.HasRows Then
  
          dqty = sqlreader("qty")
          sqlreader.Close()
  
      Else
          sqlreader.Close()
      End If
      If quan &gt; dqty Then
          Return False
      Else
          Return True
  
      End If
  End Function
  </code></pre>