The call to dataReader.Read()
advances to the next available record.
When you call dataReader[i]
, you're probably trying to get a column that doesn't exist in your returned data. On the first iteration, dataReader[i]
is trying to get the first column, but then on the second iteration it's just trying to get the second column, then third, etc. You're probably not returning 6 columns, so that's going to throw an exception.
You could try something like this instead, assuming that each record is just a string:
for(i=0; i<n; i++)
{
dataReader.Read();
db_name[i] = new TreeNode(dataReader.GetString(0));
// or use dataReader[0].ToString(), but don't change the index of 0
}