title: How to get int32 and strings from SqlDataReader. Getting error System.IndexOutOfRangeException: 'StudentID'
body: <p>I am working on a personal project that is using a Microsoft SQL DB and a web layer for Rest API calls. I am trying to get int32s and strings back from the database using a SqlDataReader. When I read in Strings there is no error, however when I attempt to read integers I get the following </p>
  
  <blockquote>
    <p>`Exception: System.IndexOutOfRangeException: 'StudentID'</p>
  </blockquote>
  
  <pre class="lang-cs prettyprint-override"><code>public StudentAccount FindStudentByID(int id)
  {
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
          connection.Open();
          String sql = "SELECT FirstName, LastName,StudentBiography FROM [PersonTest].[dbo].[StudentAccounts] WHERE StudentID = @p1";
          StudentAccount matchingStudent = new StudentAccount();
  
          using (SqlCommand cmd = new SqlCommand(sql, connection))
          {
              cmd.Parameters.Add("@p1", SqlDbType.Int).Value = id;
              cmd.CommandType = CommandType.Text;
  
              SqlDataReader reader = cmd.ExecuteReader();
  
                  while (reader.Read())
                  {
                      matchingStudent.StudentID = reader.GetInt32(reader.GetOrdinal("StudentID"));
  
                      matchingStudent.FirstName = reader["FirstName"].ToString();
                      matchingStudent.LastName = reader["LastName"].ToString();
                      matchingStudent.Biography = reader["StudentBiography"].ToString();
  
                      matchingStudent.CollegeID = reader.GetInt32(reader.GetOrdinal("COLLTB_CollegeID"));
                  }
  
              reader.Close();
  
              return matchingStudent;
          }
      }
  }
  </code></pre>
  
  <p>The StudentAccounts table is as following:</p>
  
  <pre><code>StudentAccounts (StudentID, FirstName, LastName, StudentBiography, CollTB_CollegeID)
  </code></pre>
  
  <p>The <code>StudentID</code> is the primary key.</p>
  
  <p>I apologize ahead of time if I messsed up the format or anything, this is my first time posting on this forum. Thanks! </p>