Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have error in this code ,its urgent
plzzzzzzzz anbody solve this error


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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        protected DataSet PDataset(string select_statement)
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");
            SqlDataAdapter ad = new SqlDataAdapter(select_statement, con);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            con.Close();
            return ds;
        }


        public void Load_tree2()
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");


            con.Open();

            DataSet ds = PDataset("Select * from details");
            treeView1.Nodes.Clear();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                if ((Convert.ToInt32(dr["parentname"].ToString()) == 0))
                {
                    TreeNode tnParent = new TreeNode();
                    tnParent.Text = dr["name"].ToString();
                    string value = dr["id"].ToString();
                    tnParent.Expand();
                    treeView1.Nodes.Add(tnParent);
                    FillChild(tnParent, value);
                    treeView1.ExpandAll();
                }
            }
        }

        public int FillChild(TreeNode parent, string ID)
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");

            con.Open();
            DataSet ds = PDataset("SELECT * FROM details WHERE ParentID =" + ID);
            if (ds.Tables[0].Rows.Count > 0)
            {

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode child = new TreeNode();
                    child.Text = dr["name"].ToString().Trim();
                    string temp = dr["id"].ToString();
                    child.Collapse();
                    parent.Nodes.Add(child);
                    FillChild(child, temp);
                }
                return 0;
            }
            else
            {
                return 0;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Load_tree2();

        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");

            con.Open();


            string MyString = "INSERT INTO details(name,parentname)values('" + textBox1.Text + "'," + getNodeid(treeView1.SelectedNode.ToString()) + ")";

            SqlCommand MyCmd = new SqlCommand(MyString, con);


            MyCmd.ExecuteNonQuery();

            TreeNode childnode = new TreeNode(textBox1.Text);
            treeView1.SelectedNode.Nodes.Add(childnode);
            treeView1.ExpandAll();
            textBox1.Clear();
            con.Close();
            Load_tree2();
            textBox1.Clear();

        }

        private int getNodeid(string nodename)
        {
            string node = nodename.Substring(10);
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");
            con.Open();
            string MyString = "select ID from details where name='" + node + "'";

            SqlCommand MyCmd = new SqlCommand(MyString, con);
            SqlDataAdapter da = new SqlDataAdapter(MyCmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            string aa = dt.Rows[0][0].ToString();

            con.Close();
            return Convert.ToInt32(aa);

        }

        private void btnremove_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");
            con.Open();

            string MyString = "Delete from details where name= '" + treeView1.SelectedNode.ToString() + "'";

            SqlCommand MyCmd = new SqlCommand(MyString, con);
            SqlDataAdapter da = new SqlDataAdapter(MyCmd);
            MyCmd.ExecuteNonQuery();
            Load_tree2();

            con.Close();
            //treeView1.SelectedNode.Remove();
        }





        private void btnparent_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.0.50\\CIODEVDB;user id=sa;password=epm@3108;database=Parent");
            con.Open();
            string MyString = "INSERT INTO details(name)values('" + textBox1.Text + "')";
            SqlCommand MyCmd = new SqlCommand(MyString, con);
            TreeNode parentnode = new TreeNode(textBox1.Text);
            treeView1.SelectedNode.Nodes.Add(parentnode);
            MyCmd.ExecuteNonQuery();

            con.Close();
            textBox1.Clear();
        }
    }
}
Posted
Updated 24-Jan-12 1:13am
v2
Comments
uspatel 24-Jan-12 7:14am    
share error message.
amolpatil2243 24-Jan-12 7:15am    
what is the error, post it
[no name] 4-May-12 3:11am    
Reason for my vote of 1
Explanation is not there.

the answer may be funny but let me help


try this code now

C#
string MyString = "INSERT INTO details(name,parentname) values('" + textBox1.Text + "','" + getNodeid(treeView1.SelectedNode.ToString()) + "')";


please check the syntax you have give.. be careful about the singe quote and multiple .. always make sure to close the data reader if any..
 
Share this answer
 
v2
what is the error..? Can you give me the error details?
 
Share this answer
 
Object reference not set to an instance of an object(this is the error)
 
Share this answer
 
I am not able to find the file line can you copy that line and give it to me?
 
Share this answer
 
string MyString = "INSERT INTO details(name,parentname)values('" + textBox1.Text + "'," + getNodeid(treeView1.SelectedNode.ToString()) + ")";
 
Share this answer
 
Comments
AmitGajjar 24-Jan-12 8:01am    
on which line ?

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