title: Error - "Invalid attempt to read when no data is present." while using SQLDataReader even if data is present
body: <p>I was trying to to use parameterized query with data reader when I get this error message "Invalid attempt to read when no data is present."</p>
  
  <p>But data is there is the reader!</p>
  
  <p><img src="https://i.stack.imgur.com/bgDHk.png" alt="enter image description here"></p>
  
  <p>Following is the line of code which performs this task</p>
  
  <pre><code>using (ConnectionManager connectionManager = new ConnectionManager())
  {   
      string query = @"SELECT * FROM LoginTab WHERE username=@username " +
                "AND password=@password";
  
      List&lt;SqlParameter&gt; sqlParameterCollection = new List&lt;SqlParameter&gt;();
      sqlParameterCollection.Add(new SqlParameter("@username", SqlDbType.NVarChar) { Value = userName });
      sqlParameterCollection.Add(new SqlParameter("@password", SqlDbType.NVarChar) { Value = password });
  
      SqlDataReader sqlDataReader = connectionManager.ExecuteReader(query, CommandType.Text, sqlParameterCollection);
  
      String roles = sqlDataReader[0].ToString();
      return roles;
  }
  </code></pre>
  
  <p>ExecuteReader function is defined in another class.</p>
  
  <pre><code>public SqlDataReader ExecuteReader(String strcmd, CommandType type, List&lt;SqlParameter&gt; Parametercollections)
  {
      connnection = new SqlConnection(Connnectionstring);
      command = new SqlCommand(strcmd, connnection);
      command.CommandType = type;
      foreach (SqlParameter paras in Parametercollections)
      {
          command.Parameters.Add(paras);
      }
      try
      {
          connnection.Open();
          reader = command.ExecuteReader();
      }
      catch (SqlException E)
      {
  
      }
      finally
      {
  
      }
  
      return reader;
  }
  </code></pre>
  
  <p>What could be wrong here?</p>