Click here to Skip to main content
15,897,273 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
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 17-May-12 19:21pm
v2

1 solution

You will have to build command string. here you are conflicting code for calling stored procedure with query.


Build command text and execute query.

SqlCommand com = new SqlCommand("insert into treeview_select(subdisease_name) values('" + strTreeValue + "')", con);
 
Share this answer
 
v2
Comments
shabadiveda 18-May-12 5:01am    
can u send me complete code for the same plz...
shabadiveda 18-May-12 5:31am    
with the help of break points i came to know that object "tnode" is not getting created in foreach loop, without creation of object control is coming out of loop.

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