Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am developing a project in which I am having a treeview and it is populated from the database, I want to insert the checked item in treeview to database after clicking a button, I am not getting how to insert the checked item in tree view to database.My code in button click event looks like this. Can any one help me...its very urgent...please...help me..
C#
protected void btn1_Click(object sender, EventArgs e) 
{ 
     SqlConnection con = new SqlConnection("Data Source=GAYATHRI\SQLEXPRESS;Initial      Catalog=VOY_SERVICES1;Integrated Security=True"); con.Open();
 
    foreach (TreeNode tnode in TreeView1.CheckedNodes)
    {
        if (tnode.Checked == true)
        { 
            string strTreeValue;
            strTreeValue = tnode.Value;
            SqlCommand com = new SqlCommand("insert into treeview_select(subdisease_name) values(@name)", con);
 
            com.Parameters.AddWithValue("@name", strTreeValue);
            com.ExecuteNonQuery();
            con.Close(); 
        }
    }
}
Posted
Updated 28-May-12 0:10am
v2
Comments
Deepak_Sharma_ 28-May-12 6:15am    
Are you getting any error?
shabadiveda 29-May-12 6:52am    
am not getting any error. selected item in treeview is not getting inserted into database. can any one got any answer...
Deepak_Sharma_ 29-May-12 7:14am    
Put a breakpoint on foreach () line and see step by step if strTreeValue contains a value
shabadiveda 30-May-12 0:49am    
by break point i came to know that tnode object is no getting created. any other code is also welcome.

1 solution

tnode.Value is unlikely to be the value you want to save to the DB and is probably empty. I suspect you want to save tnode.Text, which will be the value displayed in your tree.
 
Share this answer
 
Comments
shabadiveda 29-May-12 6:53am    
ya i tried tnode.Text, then also its not working

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