hi friends iam new to treeview control using
i want to save treeview checked nodes will be save in to database table
please help me..
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 28, 2013, 4:52 PM EST
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;
namespace Treeview_checkbox_checked_nodes
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com, com1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindtree();
}
}
void bindtree()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Select * from Category";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
com.Dispose();
string[,] ParentNode = new string[100, 2];
int count = 0;
while (reader.Read())
{
ParentNode[count, 0] = reader.GetValue(reader.GetOrdinal("Category_ID")).ToString();
ParentNode[count++, 1] = reader.GetValue(reader.GetOrdinal("CategoryName")).ToString();
}
reader.Close();
for (int loop = 0; loop < count; loop++)
{
TreeNode root = new TreeNode();
root.Text = ParentNode[loop, 1];
com1 = new SqlCommand("Select * from SubCategory where Category_ID =" + ParentNode[loop, 0], con);
SqlDataReader reader1 = com1.ExecuteReader();
while (reader1.Read())
{
TreeNode child = new TreeNode();
child.Text = reader1.GetValue(reader1.GetOrdinal("SubCategoryName")).ToString();
root.ChildNodes.Add(child);
}
reader1.Close();
TreeView1.Nodes.Add(root);
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
Response.Write("Seleted values of nodes:
");
foreach (TreeNode item in this.TreeView1.CheckedNodes)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Insert into Category1 values ('" + item.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Response.Write(item.Value + "
");
}
}
}
}
chandu gummadi57 reputationPosted Feb 28, 2013, 8:21 PM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 28, 2013, 8:34 PM EST
Category table
Category_ID varchar(50)
CategoryName varchar(50)
SubCategory Table
SubCategory_ID varchar(50)
SubCategoryName varchar(50)
Category_ID varchar(50)
------
Category1 table
CategoryName varchar(50)
chandu gummadi57 reputationPosted Feb 28, 2013, 9:10 PM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 1, 2013, 5:50 AM EST
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;
namespace Treeview_checkbox_checked_nodes
{
public partial class WebForm1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void btn_save_Click(object sender, EventArgs e)
{
Response.Write("Seleted values of nodes:
");
foreach (TreeNode item in this.TreeView1.CheckedNodes)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Insert into item values ('" + item.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Response.Write(item.Value + "
");
}
}
}
}
Table is item
items varchar(50)