I have the following data:

This is my code:
[code]
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT MaPhong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, 0, null);
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["TenPhong"].ToString(),
Value = row["MaPhong"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
[/code]
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT MaPhong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, 0, null);
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["TenPhong"].ToString(),
Value = row["MaPhong"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
[/code]
Relations between the two tables with the data type 'string' to error:

This is table parent node:

This is table child node:

This is database exam:
I want load database to treeview (user follow room). Help me!Thank
Cao Cu HaoPosted Aug 6, 2015, 10:24 PM
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT MaPhong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, string.Empty, null);
}
}
private void PopulateTreeView(DataTable dtParent, string parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row[1].ToString(),
Value = row[0].ToString()
};
if (string.IsNullOrEmpty(parentId))
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong ='" + child.Value + "'");
PopulateTreeView(dtChild, child.Value, child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
Upendra Pratap ShahiPosted Aug 6, 2015, 4:58 AM
int result;int.TryParse(error, out result);Manas MohapatraPosted Aug 6, 2015, 4:53 AM
Use result variable in populate tree view. But one problem here is if child.value is not a number then you always get 0 as a result.Cao Cu HaoPosted Aug 6, 2015, 4:23 AM
Upendra Pratap ShahiPosted Aug 6, 2015, 1:00 AM
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT MaPhong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, 0, null);
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["TenPhong"].ToString(),
Value = row["MaPhong"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong = '" + child.Value + "'");
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
Manas MohapatraPosted Aug 6, 2015, 12:36 AM