hi all
I need help plzzzzzzzzzzzzzzzz
my project uses asp.net and sql database
I use treeview to insert and retrieve hierarchical data from the database
I used the following code the problem is when I added the code for the retrieval the insertion has a problem where I can't add any child to the tree however it was working well before adding that code started from the fill_tree2
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
//using system.input
public partial class Study_period : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(@"Data Source=SONY-PC;Initial Catalog=GP;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
fill_Tree2();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
cn.Open();
//string xxx = "insert into ParentTable values('" + TextBox3.Text + "')";
string xxx = "insert into ParentTable values('" + TextBox3.Text + "','" + DropDownList2.SelectedValue + "')";
SqlCommand cmd = new SqlCommand(xxx, cn);
cmd.ExecuteNonQuery();
cn.Close();
TreeNode parentnode = new TreeNode(TextBox3.Text);
TreeView1.Nodes.Add(parentnode);
}
catch (SqlException ex)
{
Label1.Text = ex.Message;
}
}
protected void Button4_Click(object sender, EventArgs e)
{
String sql = "select ParentID from ParentTable where ParentName ='" + TreeView1.SelectedValue + "'";
// SqlDataAdapter da = new SqlDataAdapter(sql, cn);
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
int x = Convert.ToInt16(cmd.ExecuteScalar());
cn.Close();
Response.Write(x.ToString());
}
protected void Button3_Click(object sender, EventArgs e)
{
String sql = "select ParentID from ParentTable where ParentName='" + TreeView1.SelectedValue + "' ";
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
int x = Convert.ToInt16(cmd.ExecuteScalar());
cn.Close();
x += 0;
TextBox5.Text = x.ToString();
string xx = "insert into ChildTable values(" + x + ",'" + TreeView1.SelectedValue + "','" + TextBox4.Text + "','" + DropDownList2.SelectedValue + "')";
cn.Open();
SqlCommand cmd1 = new SqlCommand(xx, cn);
cmd1.ExecuteNonQuery();
cn.Close();
TreeNode childnode = new TreeNode(TextBox4.Text);
TreeView1.SelectedNode.ChildNodes.Add(childnode);
}
void fill_Tree2()
{
DataSet PrSet = PDataset("Select * from ParentTable");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["ParentName"].ToString();
tnParent.Value = dr["ParentID"].ToString();
tnParent.PopulateOnDemand = true;
tnParent.ToolTip = "Click to get Child";
tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
tnParent.Selected = true;
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
}
}
public void FillChild(TreeNode parent, string ParentId)
{
DataSet ds = PDataset("Select * from ChildTable where ParentID =" + ParentId);
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["ChildName"].ToString().Trim();
child.Value = dr["ParentID"].ToString().Trim();
if (child.ChildNodes.Count == 0)
{
child.PopulateOnDemand = true;
}
child.ToolTip = "Click to get Child";
child.SelectAction = TreeNodeSelectAction.SelectExpand;
child.CollapseAll();
parent.ChildNodes.Add(child);
}
}
protected DataSet PDataset(string Select_Statement)
{
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement,cn);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
}