0
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? :/

2
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by AleMonteiro
0

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.

Edited by AleMonteiro: bad english

0

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? @@

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.