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]
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