title: There is no row at position 0 error at asp.net
body: <p>I was writing a web based program and this is my authentication page. It was working fine but suddenly it started to give that error. </p>
  
  <p>Here is my code:</p>
  
  <pre><code> else if (LoginAs.SelectedValue == "Student")
          {
              string tableName = "StudentTable";
              String name = "", surname = "", email = "";
              string query = "Select level from " + tableName + " where ID='" + idBox.Text + "'";
              SqlCommand cmd = new SqlCommand(query, con);
              string level = Convert.ToString(cmd.ExecuteScalar());
              CreateUser(con, tableName, ref name, ref surname, ref email);
              query = "Select program from " + tableName + " where ID='" + idBox.Text + "'";
              cmd = new SqlCommand(query, con);
              string program = Convert.ToString(cmd.ExecuteScalar());
              MyGlobals.student = new Student(Convert.ToInt32(idBox.Text), "Active", email, name, surname, password, level, program);
  
              MyGlobals.currentID = idBox.Text;
              query = "Select * from RegisterTable where StudentID='" + idBox.Text + "'";
  
              cmd = new SqlCommand(query, con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
  
              DataTable dt = new DataTable();
              da.Fill(dt);
  
              foreach (DataRow dr in dt.Rows)
              {
                  query = "SELECT * FROM CourseTable WHERE CourseCode='" + dr["CourseCode"] + "' AND CourseNumber='" + dr["CourseNumber"] + "' AND Term='" + dr["Term"] + "'";
                  cmd = new SqlCommand(query, con);
                  SqlDataAdapter da2 = new SqlDataAdapter(cmd);
  
                  DataTable dt2 = new DataTable();
                  da2.Fill(dt2);
                  DataRow dr2 = dt2.Rows[0];  //ERROR COMES AT HERE
  
                  Course course = new Course(dr2["InstructorName"].ToString(), dr2["CourseCode"].ToString(), dr2["CourseNumber"].ToString(), dr2["CourseName"].ToString(), dr2["Term"].ToString(), dr2["CRN"].ToString(), dr2["Level"].ToString(), dr2["Credit"].ToString(), dr2["Description"].ToString(), dr2["Capacity"].ToString());
                  Register reg = new Register(course, MyGlobals.student);
  
                  MyGlobals.student.addToSchedule(reg);
              }
              int num = (int)Application["OnlineUsers"];
              Response.Redirect("Student.aspx");
          }
  </code></pre>
  
  <p>Can anyone help me with this? Thanks in advance.</p>