I’m having some trouble figuring out why I can only get the children of one parent in my treeview to be added. I’m sure it’s something simple I’m forgetting, but I’m a bit stumped. I put **’s around the loop I’m having trouble with but included the rest of the code in case there’s something wrong there.
Code:
string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=PartsTree.accdb"; //2007 db format
//string connStr = @"Provider=Microsoft.JET.OLEDB.4.0; Data Source=PartsTree.accdb"; //2003 db format
try
{
using (OleDbConnection dbConn = new OleDbConnection(connStr))
{
string queryString = "SELECT ID, parent_ID, description FROM Parts;"; //Note two semicolons
dbConn.Open();
OleDbCommand dbCmd = dbConn.CreateCommand();
dbCmd.CommandText = queryString;
OleDbDataReader rdr = dbCmd.ExecuteReader();
int numberColumns = rdr.FieldCount; // How many columns are there in the DB
for (int x = 0; x < numberColumns; x++)
{
treeView1.Nodes.Add(rdr.GetName(x)); // Add a parent node to the tree for each column
}
**for (int y = 0; y <= numberColumns; y++)
{
while (rdr.Read())
{
string childData = rdr[y].ToString();
treeView1.Nodes[y].Nodes.Add(childData);
}
}**
rdr.Close();
//while (rdr.Read())
//{
// string blah = rdr[2].ToString();
// treeView1.Nodes[0].Nodes.Add(blah);
// }
// rdr.Close();
dbConn.Close();
}
}
catch (OleDbException y)
{
Console.Error.WriteLine("Error: Failed to create a database connection. \n{0}", y.Message);
}
Flip the loop: