title: Invalid attempt to read when no data is present in ASP.NET
body: <p>I am using C# to learn ASP.NET, I tried to write authentication code as</p>
  
  <pre><code>SqlCommand command = new SqlCommand("Select [ID] from [Inspector] WHERE [ID] ='111' AND [Password] ='111';", conn);
  
  SqlDataReader dr = command.ExecuteReader();
  
  if (dr[0].ToString() == username)
  {
  Session["UserAuthentication"] = username;
  Session.Timeout = 1;
  Response.Redirect("About.aspx");
  }
  else{
  // ...
  }
  </code></pre>
  
  <p>The <code>ID</code> is of type <code>numeric</code> in SQL Server. </p>
  
  <p>The issue is I always get exception:</p>
  
  <blockquote>
    <p>Invalid attempt to read when no data is present.</p>
  </blockquote>
  
  <p>In this part: <code>if (dr[0].ToString() == username)</code>.</p>
  
  <p>I tried to execute the exact SQL statement directly and I can get the result.</p>