private void getStudentInformation()
        {
            SqlConnection conOOC = new SqlConnection(ConfigurationManager.ConnectionStrings["OlympiaOpenCollegeDatabaseConnection"].ConnectionString);
            SqlCommand cmdSelectStudent = new SqlCommand("SELECT l.LearnerID, l.Name, l.Email FROM Learner l, Registration r WHERE l.LearnerID = r.LearnerID AND r.RegistrationID = @registerID" , conOOC);
            SqlDataReader dtrStudent;

            conOOC.Open();
            cmdSelectStudent.Parameters.AddWithValue("@registerID", "R001");
            dtrStudent = cmdSelectStudent.ExecuteReader();

            while (dtrStudent.Read())
            {
                lblStudentID.Text = dtrStudent["l.LearnerID"].ToString();[Note: Error Occured]
                lblStudentName.Text = dtrStudent["l.Name"].ToString();
                lblEmail.Text = dtrStudent["l.Email"].ToString();
            }

            conOOC.Close();
        }

IndexOutOfRangeException was unhandled by user code

What's is the problem? :/

Recommended Answers

All 3 Replies

Even when you use aliases on your query, you don't use it to get the value from the reader.

while (dtrStudent.Read())
            {
                lblStudentID.Text = dtrStudent["LearnerID"].ToString();
                lblStudentName.Text = dtrStudent["Name"].ToString();
                lblEmail.Text = dtrStudent["Email"].ToString();
            }

The above should work fine.

i've solved the problem already. thank you :D
by the way, how do it know which one i refer to if i didn't put l.[Something] to retrieve value? @@

Always use different aliases, ie: SELECT p.ID as personID, c.ID as CompanyID...
row["personID"] and row["companyID"]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.