0

I'm trying to create a tree with the following very small access database.
I'm still getting a few errors,
'rdr' is a 'variable' but used like a 'method'
and trying to get tree to actually populate..
If you find any other errors or recommendations, they're greatly appreciated.
Thanks a lot.

DatabaseName is PartsTree
TableName is Parts

ID parent_ID description
1------null------Car
2------1------Wheels
3------2------Rims
4------2------Hubcabs
5------2------Lugnuts
6------1------Engine
7------6------Pistons
.------.------.
.------.------.
.------.------.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void partsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.partsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.partsTreeDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'partsTreeDataSet.Parts' table. You can move, or remove it, as needed.
            this.partsTableAdapter.Fill(this.partsTreeDataSet.Parts);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            partsTableAdapter.Connection.Open();
            treeView1.Nodes.Clear();
            fillTree("1", null, "Car");
            partsTableAdapter.Connection.Close();
        }

        public void fillTree(String Key, TreeNode N, String Txt)
        {
            OleDbConnection dbConn;
            OleDbCommand dbCmd;
            TreeNode NN;

            if (N == null)
            {
                NN = treeView1.Nodes.Add(Key, Txt);
            }
            else
            {
                NN = N.Nodes.Add(Key, Txt);
            }
            dbConn = partsTableAdapter.Connection;
            dbCmd = new OleDbCommand("SELECT * FROM Parts WHERE parent_ID ='" + Key + "'", dbConn);
            OleDbDataReader rdr = dbCmd.ExecuteReader();
            while (rdr.Read()){
                fillTree(rdr("ID"), rdr("parent_ID"), NN);
                }
            rdr.Close();
            dbCmd.Dispose();
        }
    }
}
2
Contributors
1
Reply
5
Views
7 Years
Discussion Span
Last Post by __avd
0

Take a look at,

SqlDataAdapter adp = new SqlDataAdapter("select * from tableName", @"connection_str");
 DataTable dt = new DataTable();
 adp.Fill(dt);


 foreach (DataRow row in dt.Rows)
  {
  string key = row["pid"].ToString();
  TreeNode[] tn = treeView1.Nodes.Find(key, true);
  if (tn.Length ==0)
  {
    treeView1.Nodes.Add(row["id"].ToString(), row["desc"].ToString());
   }
  else
  {
    tn[0].Nodes.Add(row["id"].ToString(), row["desc"].ToString());
   }
}

Edited by __avd: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.