Click here to Skip to main content
15,886,919 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...

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();
 
        }
    }
}


[edit]Code block added, Urgency deleted - OriginalGriff[/edit]
Posted
Updated 14-Jun-12 21:04pm
v2
Comments
OriginalGriff 15-Jun-12 3:04am    
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.
shabadiveda 15-Jun-12 3:56am    
ok, bt make it as soon as possible, thanx.
Maciej Los 15-Jun-12 4:09am    
OK, but what's wrong, where is the problem?
shabadiveda 15-Jun-12 5:06am    
my problem is that selected node is not getting inserted into the database.

On the first look, youre code seems to be OK, but...
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();
            com.Dispose();
            //con.Close(); if connection is closed, the command will be executed only once!
        }
    }
con.Close();
com = Nothing;
con = Nothing;
}


Have a nice day ;)
 
Share this answer
 
Comments
Manas Bhardwaj 15-Jun-12 4:19am    
correct +5!
Maciej Los 15-Jun-12 12:43pm    
Thank you, Manas ;)
amolpatil2243 15-Jun-12 6:10am    
yeah!!! right, my +5!
Maciej Los 15-Jun-12 12:44pm    
Thank you ;)
Sandeep Mewara 20-Jun-12 8:14am    
Good answer. 5!
yes,Permalink,sure, //con.Close(); if connection is closed, the command will be executed only once!...
 
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