title: How to get the ID number from a selected treeView node?
body: <p>I have a <code>TreeView</code> that is populated with data from a SQL Server database table, I want to insert data into it based on the selected item ID in the database, this is my code:</p>
  
  <pre><code>    private void Form1_Load(object sender, EventArgs e)
      {
          TreeLoad();
      }
  
      // Connecting to Database and retieving data
      private DataTable Select(int ID)
      {
          SqlConnection conn = new SqlConnection(@"server= M_SHAWAF\ORCHESTRATE; integrated security= true; database= FM");
          conn.Open();
          SqlCommand cmd = new SqlCommand("GetNodes2", conn);
          cmd.CommandType = CommandType.StoredProcedure;
          SqlParameter parameter = new SqlParameter("ID", ID);
          parameter.SqlDbType = SqlDbType.Int;
          cmd.Parameters.Add(parameter);
          SqlDataAdapter ad = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          ad.Fill(dt);
          return dt;
      }
  
      //Loading the treeView1
      void TreeLoad()
      {
          DataTable dt = Select(0);
          AddNodes(dt, treeView1.Nodes);
      }
  
      //Adding nodes to treeView1
      void AddNodes(DataTable dt, TreeNodeCollection coll)
      {
          foreach (DataRow dr in dt.Rows)
          {
              TreeNode node = new TreeNode();
              node.Text = dr[1].ToString();
              coll.Add(node);
  
              try
              {
                  DataTable child = Select((int)dr[0]);
  
                  if (child.Rows.Count &gt; 0)
                  {
                      AddNodes(child, node.Nodes);
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
      }
  </code></pre>
  
  <p>This is the table:</p>
  
  <p><img src="https://i.stack.imgur.com/p3dY0.png" alt="enter image description here"></p>
  
  <p>The form:</p>
  
  <p><img src="https://i.stack.imgur.com/eAuCh.png" alt="enter image description here"></p>
  
  <p>I want to get the <code>EmpID</code> of the selected node when I click insert btn (there is another windows form that has a textbox for the new name).</p>
  
  <p><code>GetNodes</code> stored procedure:</p>
  
  <pre><code>create procedure GetNodes 
     @ID int
  as  
     select 
        e.EmpID, e.EmpName 
     from 
        Employee e 
     where 
        isnull(e.ManagerID, 0) = @ID 
  
     return
  </code></pre>