Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
this is my code....

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length != 0)
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);
                    TreeNode parentNode = new TreeNode(textBox1.Text);
                    treeView1.Nodes.Add(parentNode);
                    textBox1.Clear();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    BindDataInTreeView();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    con.Close();
                }
            }
            else
            {
                MessageBox.Show("");
            }
        }

        private void btnchild_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {

                if (textBox1.Text.Length != 0)
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);
                        TreeNode childNode = new TreeNode(textBox1.Text);
                        treeView1.SelectedNode.Nodes.Add(childNode);
                        treeView1.ExpandAll();
                        textBox1.Clear();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        BindDataInTreeView();
                    }
                     catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    con.Close();
                }
            }
            else
            {
                MessageBox.Show("");
            }
                }
            }
        

        private void BindDataInTreeView()
        {
            treeView1.Nodes.Clear();
            try
            {
                con.Open();
              
                SqlCommand cmd = new SqlCommand("SELECT parentNode,childNode FROM treenodes", con);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
               
                    treeView1.Nodes.Add(dr.GetValue(0).ToString());
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                con.Close();
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            if (treeView1.Nodes.Count > 0)
            {
               
                if (treeView1.SelectedNode != null)
                {
                
                    treeView1.SelectedNode.Remove();
                    MessageBox.Show("Node Removed Successfully", "Success Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                else
                {

                    MessageBox.Show("Please Select Any Node To Be Deleted.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                }

            }

            else
            {

                MessageBox.Show("There Is No Node In The Tree View Control.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
        }

        
    }
}
Posted
Comments
Herman<T>.Instance 14-Mar-12 9:18am    
what is your exact question? what errors do you run into?
rockpune 14-Mar-12 9:24am    
when i run iam getting one message box..
am trying to insert parentnode and child node into database
rockpune 14-Mar-12 9:26am    
in that meassagebox getting code
Herman<T>.Instance 14-Mar-12 9:29am    
and the code is?
rockpune 14-Mar-12 9:45am    
nothing but exceptions

1 solution

There are a couple things going on here:

1) You have already declared SqlCommand cmd at the class level, so you do not need to declare it again (you should get an exception for this). Just assign the cmd variable.
C#
cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);


2) When inserting strings into sql, the value must be surrounded by single quote marks (you should get an exception for this).
C#
cmd = new SqlCommand("insert into treenodes values('" + textBox1.Text + "');", con);

Note: It would be better to use SqlCommand.Parameters to add the values, then they would get wrapped appropriately if needed. It is also good practice for avoiding sql injection attacks.

3) Since you do not specify which column to insert into, the value will always insert into the first column. When you retrieve the data:
C#
cmd = new SqlCommand("SELECT parentNode,childNode FROM treenodes", con);

if the parentNode is the first column, then all the values you entered will be in that column and the childNode column will be full of NULL values. To specify which column to insert values to:
C#
cmd = new SqlCommand("insert into treenodes (parentNode) values('" + textBox1.Text + "');", con);

or, if you enter a child node, you will want to specify who the parent node is:
C#
cmd = new SqlCommand("insert into treenodes (parentNode, childNode) values('" + treeView1.SelectedNode.Text + "','" + textBox1.Text + "');", con);


4) The way you are looping through the results, you are adding all the values as if they were parents, you are not adding the child nodes to the proper parent.
C#
while (dr.Read())
{

     treeView1.Nodes.Add(dr.GetValue(0).ToString());
}

This should give you a few things to look at.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900