Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My datatable -

Id | name | parent id

1 | System | 0

2 | user | 1

3 | main | 0

4 | course | 3

5 | semester | 4

my code -

string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from menu_details";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
    if (Reader[2].ToString() == Convert.ToString(0))
    {
        treeView1.Nodes.Add(Reader[3].ToString(), Reader[1].ToString());
    }

    if (Reader[2].ToString() == Convert.ToString(1))
    {
        treeView1.Nodes[0].Nodes.Add(Reader[3].ToString(), Reader[1].ToString());
    }

    if (Reader[2].ToString() == Convert.ToString(3))
    {
        treeView1.Nodes[1].Nodes.Add(Reader[3].ToString(), Reader[1].ToString());
    }

    if (Reader[2].ToString() == Convert.ToString(4))
    {
         treeView1.Nodes[1].Nodes[0].Nodes.Add(Reader[3].ToString(), Reader[1].ToString());
    }
}
connection.Close();


but it throw an error,

Specified argument was out of the range of valid values. Parameter name: index

at,

treeView1.Nodes1.Nodes[0].Nodes.Add(Reader[3].ToString(), Reader1.ToString());

what is the problem in my code.?.

Is any other way to simplify code for my requirement?

thanks in advance!@.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900