title: Invalid attempt to read when no data is present
body: <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          string name;
          name = textBox5.Text;
          SqlConnection con10 = new SqlConnection(&quot;con strn&quot;);
          SqlCommand cmd10 = new SqlCommand(&quot;select * from sumant where username=@name&quot;);
          cmd10.Parameters.AddWithValue(&quot;@name&quot;,name);
          cmd10.Connection = con10;
          cmd10.Connection.Open();//line 7
          SqlDataReader dr = cmd10.ExecuteReader();
      }
  
      if ( textBox2.Text == dr[2].ToString())
      {
          //do something;
      }
  </code></pre>
  <p>When I debug until line 7, it is OK, but after that <code>dr</code> throws an exception:</p>
  <blockquote>
  <p>Invalid attempt to read when no data is present.</p>
  </blockquote>
  <p>I don't understand why I'm getting that exception, since I do have data in the table with username=sumant.</p>
  <p>Please tell me whether the 'if' statement is correct or not. And how do I fix the error?</p>