title: Creating treeview from database
body: <p>I have a database named dtEmploye with fields </p>
  
  <blockquote>
    <p><strong>employe_id</strong><br>
    <strong>name</strong><br>
    <strong>surname</strong><br>
    <strong>boss_id</strong> <em>which has a recursive relation on <strong>employee_id</strong> where I have in code XXX</em></p>
  </blockquote>
  
  <p>I have the code but there is some error it shows "Specified cast is not valid."</p>
  
  <pre><code>private void btntree_Click(object sender, EventArgs e)
      {
          DataTable vec = sel("SELECT * FROM dtemployee");
          treeView1.Nodes.Clear();
          foreach (DataRow dr in vec.Rows)
          {
              if ((int)dr["boss_id"] == XXX)
              {
                  TreeNode parent = new TreeNode();
                  parent.Text = dr["name"+"surname"].ToString();
                  string value = dr["employe_id"].ToString();
                  parent.Expand();
                  treeView1.Nodes.Add(parent);
                  sublevel(parent, value);
              }
          }
      }
      public int sublevel(TreeNode parent, string id)
      {
          DataTable ch = sel("SELECT * FROM dtEmploye WHERE boss_id=" + id);
          if (dtEmploye.Rows.Count &gt; 0)
          {
              foreach (DataRow dr in ch.Rows)
              {
                  TreeNode child = new TreeNode();
                  child.Text = dr["name"+"surname"].ToString().Trim();
                  string temp = dr["employe_id"].ToString();
                  child.Collapse();
                  parent.Nodes.Add(child);
                  sublevel(child, temp);
              }
              return 0;
          }
          else
          {
              return 0;
          }
      }
      protected DataTable sel(string select)
      {
          NpgsqlDataAdapter adpt = new NpgsqlDataAdapter(select, con);
          DataTable dt2 = new DataTable();
          adpt.Fill(dt2);
          return dt2;
      }
  </code></pre>