.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Save selected Tree view checked nodes to database

Posted By:satyapriyanayak       Posted Date: June 04, 2014    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to save selected Tree view checked nodes to database.
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Treeview_checkbox_checked_nodes._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     SaveSelectedValue:<br/>
        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
        </asp:TreeView>
        <br />
        <asp:Button ID="btn_save" runat="server" Text="SaveSelectedValue"
            onclick="btn_save_Click"/>

    </div>
    </form>
</body>
</html>


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("<b>Seleted values of nodes:</b><br/>");
            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 + "<br/>");
            }
        }

       
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend