Mercvapor
September 26th, 2010, 07:00 PM
Here the code I've been having problems with.
When my Reader[count] goes through the second loop which is Reader[1].
It says index out of bounds, but there is suppose to be 3 rows of data with a single column each.
anyone knows what's the problem?
public int getEvalRows(string UserID)
{
int rows = 0;
string evalID;
int count = 0;
SqlConnection dBCon = new SqlConnection(strConstr);
dBCon.Open();
string sqlCmd = "SELECT enrollmentID FROM [EES].[dbo].[EnrollmentStudent] Where userID = '"+UserID+"' and (doneLecture = 'False' or doneTutor = 'False')";
SqlCommand strCmd = new SqlCommand(sqlCmd, dBCon);
SqlDataReader reader = strCmd.ExecuteReader();
while (reader.Read())
{
sqlCmd = "SELECT [evaluationID] FROM [EES].[dbo].[Enrollment]where enrollmentID = '" + reader[count] + "'";
SqlConnection dBCon1 = new SqlConnection(strConstr);
dBCon1.Open();
SqlCommand strCmd1 = new SqlCommand(sqlCmd, dBCon1);
evalID = (string)strCmd1.ExecuteScalar();
sqlCmd = "SELECT evaluationID FROM [EES].[dbo].[Evaluation] where evaluationID = '" + evalID + "' and startDate < '2010-01-2' and endDate > '2010-01-2'";
SqlConnection dBCon2 = new SqlConnection(strConstr);
dBCon2.Open();
SqlCommand strCmd2 = new SqlCommand(sqlCmd, dBCon2);
SqlDataReader reader1 = strCmd2.ExecuteReader();
while (reader1.Read())
{
rows++;
}
count++;
}
return rows;
}
When my Reader[count] goes through the second loop which is Reader[1].
It says index out of bounds, but there is suppose to be 3 rows of data with a single column each.
anyone knows what's the problem?
public int getEvalRows(string UserID)
{
int rows = 0;
string evalID;
int count = 0;
SqlConnection dBCon = new SqlConnection(strConstr);
dBCon.Open();
string sqlCmd = "SELECT enrollmentID FROM [EES].[dbo].[EnrollmentStudent] Where userID = '"+UserID+"' and (doneLecture = 'False' or doneTutor = 'False')";
SqlCommand strCmd = new SqlCommand(sqlCmd, dBCon);
SqlDataReader reader = strCmd.ExecuteReader();
while (reader.Read())
{
sqlCmd = "SELECT [evaluationID] FROM [EES].[dbo].[Enrollment]where enrollmentID = '" + reader[count] + "'";
SqlConnection dBCon1 = new SqlConnection(strConstr);
dBCon1.Open();
SqlCommand strCmd1 = new SqlCommand(sqlCmd, dBCon1);
evalID = (string)strCmd1.ExecuteScalar();
sqlCmd = "SELECT evaluationID FROM [EES].[dbo].[Evaluation] where evaluationID = '" + evalID + "' and startDate < '2010-01-2' and endDate > '2010-01-2'";
SqlConnection dBCon2 = new SqlConnection(strConstr);
dBCon2.Open();
SqlCommand strCmd2 = new SqlCommand(sqlCmd, dBCon2);
SqlDataReader reader1 = strCmd2.ExecuteReader();
while (reader1.Read())
{
rows++;
}
count++;
}
return rows;
}